VBA Excel,inputbox与整数不匹配
我的代码
Dim a As Integer a = InputBox("Enter the number", "Program", "", 7000, 6000) If a = Empty Then ' do code... Else MsgBox "Enter the number." End If
如果我留下一个空字段,Excel将返回Type Mismatch
错误。 我想显示一条消息。
由于a
是一个Integer
,它不能包含一个String
或是Empty
。 使用Variant
,然后检查以查看已经返回的内容:
Dim a As Variant Dim b As Integer a = InputBox("Enter the number", "Program", "", 7000, 6000) If Not IsNumeric(a) Then 'a is not a number Else 'a is a number and can be converted to Integer b = CInt(a) End If
你有a
定义为Integer
。 Integer
不能为空。 使用Variant
而不是Integer
:
Dim a As Variant a = InputBox("Enter the number", "Program", "", 7000, 6000) If a = Empty Then ' do code... Else MsgBox "Enter the number." End If