我已经阅读了有关此问题的一些问题和文章,尽管由于我是一个初学者,所以我不知道自己的解决方案。
当用户单击InputBox
表单上的“取消”时,我需要退出sub 。另外,我需要InputBox
接受输入值。
Dim UserCol As String
Dim FirstRow As Integer
UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)
If UserCol = False Then Exit Sub
' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!
FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
If FirstRow = False Then Exit Sub
' On both cancel & input works flawlessly.
我尝试删除,Type := 2
但没有任何变化。
您不能将字符串视为布尔值(正在做什么)。字符串可以输出正确/错误的结果,但不能输出正确的结果。尝试使用以下代码:
Dim UserCol As String
Dim FirstRow As Integer
UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)
If Len(Trim(UserCol)) < 1 Then Exit Sub
' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!
FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
If FirstRow < 1 Then Exit Sub
Sub
如果(“修剪”)输入字符串的长度小于1,则第一个条件为假(并且退出)。如果输入字符串不是数字,则第二个条件为假。
注意:请记住,第二个条件不触发错误的原因是因为整数确实“支持布尔值”;尽管此处没有任何实际含义:如果删除此条件,则不会发生任何变化。我的条件检查您真正想要的(行大于或等于1)。还要记住,它InputBox
支持整数,但是通常不是这种情况(对于大多数此类控件,您必须将输入作为字符串并将其转换为整数;无论是明示还是暗含)。
更新 -
可以算出“取消”按钮的点击次数:
Dim UserCol As String
Dim FirstRow As Integer
UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)
If (LCase(UserCol) <> "false") Then
If Len(Trim(UserCol)) < 1 Then Exit Sub
' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!
FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
If (FirstRow < 1) Then Exit Sub
End If
如果第一个InputBox被取消,则返回“ False”(作为字符串),如果第二个InputBox被取消,则返回0(因此原始条件可以处理)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句