VBA Excel,输入框不匹配为整数

时间:2013-02-11 10:02:42

标签: excel vba excel-vba

我的代码

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错误。我想显示一条消息。

2 个答案:

答案 0 :(得分:15)

由于aInteger,因此不能包含StringEmpty。使用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

答案 1 :(得分:5)

您将a定义为IntegerInteger不能为空。使用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
相关问题