如何将输入框响应限制在特定范围内
How to limit inputbox response to specific range
VBA 世界的新手。另一个线程有助于创建 InputBox
和设置特定范围,但不完全是我需要的。
我让用户在 InputBox
中输入行号,他们想在其中添加一行,但我想限制他们从第 12 行(我的数据开始的地方)输入的值总行上方的行。我如何引用总行,因为它会随着行的添加而改变?以及如何使默认行位于总行上方? (目前我的总行是第22行)
Dim varUserInput As Variant
varUserInput = InputBox("Please enter the row number where you'd like to add a row:", _
"What Row?")
If varUserInput = "" Then Exit Sub
If Not IsNumeric(varUserInput) Or varUserInput < 12 Or 22 < varUserInput
Then 'If value out of specified range
varUserInput = 22 'Default value
MsgBox "You Entered a wrong value, using default", vbOKOnly
End If
将 22 与 lastrow
的值交换:
Sub Test()
Dim varUserInput As Variant, sht As Worksheet, lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1") 'specify sheet here
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'referencing column A
varUserInput = InputBox("Please enter the row number where you'd like to add a row:", _
"What Row?")
If varUserInput = "" Then Exit Sub
If Not IsNumeric(varUserInput) Or varUserInput < 12 Or lastrow < varUserInput Then 'If value out of specified range
varUserInput = lastrow 'Default value
MsgBox "You Entered a wrong value, using default", vbOKOnly
End If
End Sub
您可以尝试在调用 InputBox
时设置默认参数到 最后一行 (或其他)。
然后更改提示引导用户接受哪些数字集。
如下所示:
Sub marine()
Dim varUserInput As Variant
Dim lr As Long, myprompt As String
With Sheet1 '/* change to your actual sheet */
lr = .Range("A" & .Rows.Count).End(xlUp).Row '/* checks column A, change to suit*/
myprompt = "Please enter the row number where you'd like to add a row:"
Do
'/* enter your last row as default */
varUserInput = InputBox(myprompt, "What Row?", lr)
myprompt = "You entered an invalid row number." & vbNewLine & _
"Please enter row number between 12 and " & lr
If varUserInput = "" Then Exit Do '/* if user cancels */
If Not IsNumeric(varUserInput) Then
myprompt = "Invalid entry, numeric value expected." & vbNewLine & _
"Please enter numbers between 12 and " & lr & "."
End If
Loop Until varUserInput <= lr And varUserInput >= 12
End With
End Sub
既然你说你可以输入大于总行数的值,我不太明白你想做什么?那么这将限制用户在您的数据范围内添加行(第 12 行和最后一行)。如果这不是您需要的,请调整 Loop Until
行中的条件以满足您的需要。希望这能让你继续前进。
VBA 世界的新手。另一个线程有助于创建 InputBox
和设置特定范围,但不完全是我需要的。
我让用户在 InputBox
中输入行号,他们想在其中添加一行,但我想限制他们从第 12 行(我的数据开始的地方)输入的值总行上方的行。我如何引用总行,因为它会随着行的添加而改变?以及如何使默认行位于总行上方? (目前我的总行是第22行)
Dim varUserInput As Variant
varUserInput = InputBox("Please enter the row number where you'd like to add a row:", _
"What Row?")
If varUserInput = "" Then Exit Sub
If Not IsNumeric(varUserInput) Or varUserInput < 12 Or 22 < varUserInput
Then 'If value out of specified range
varUserInput = 22 'Default value
MsgBox "You Entered a wrong value, using default", vbOKOnly
End If
将 22 与 lastrow
的值交换:
Sub Test()
Dim varUserInput As Variant, sht As Worksheet, lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1") 'specify sheet here
lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'referencing column A
varUserInput = InputBox("Please enter the row number where you'd like to add a row:", _
"What Row?")
If varUserInput = "" Then Exit Sub
If Not IsNumeric(varUserInput) Or varUserInput < 12 Or lastrow < varUserInput Then 'If value out of specified range
varUserInput = lastrow 'Default value
MsgBox "You Entered a wrong value, using default", vbOKOnly
End If
End Sub
您可以尝试在调用 InputBox
时设置默认参数到 最后一行 (或其他)。
然后更改提示引导用户接受哪些数字集。
如下所示:
Sub marine()
Dim varUserInput As Variant
Dim lr As Long, myprompt As String
With Sheet1 '/* change to your actual sheet */
lr = .Range("A" & .Rows.Count).End(xlUp).Row '/* checks column A, change to suit*/
myprompt = "Please enter the row number where you'd like to add a row:"
Do
'/* enter your last row as default */
varUserInput = InputBox(myprompt, "What Row?", lr)
myprompt = "You entered an invalid row number." & vbNewLine & _
"Please enter row number between 12 and " & lr
If varUserInput = "" Then Exit Do '/* if user cancels */
If Not IsNumeric(varUserInput) Then
myprompt = "Invalid entry, numeric value expected." & vbNewLine & _
"Please enter numbers between 12 and " & lr & "."
End If
Loop Until varUserInput <= lr And varUserInput >= 12
End With
End Sub
既然你说你可以输入大于总行数的值,我不太明白你想做什么?那么这将限制用户在您的数据范围内添加行(第 12 行和最后一行)。如果这不是您需要的,请调整 Loop Until
行中的条件以满足您的需要。希望这能让你继续前进。