VBA Excel,inputbox作为整数不匹配

VBA Excel,inputbox作为整数不匹配,vba,excel,Vba,Excel,我的代码 如果我留下一个空字段,Excel将返回一个类型不匹配错误。我想显示一条消息。您将a定义为Integer整数不能为空。使用变量代替整数: 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 由于a是一个整数,因此它不能包含字符串或为

我的代码


如果我留下一个空字段,Excel将返回一个
类型不匹配
错误。我想显示一条消息。

您将
a
定义为
Integer
<代码>整数不能为空。使用
变量
代替
整数

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

由于
a
是一个
整数
,因此它不能包含
字符串
为空
。使用
变体
,然后检查返回的内容:

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

+1为第一个正确答案-虽然你确实想要
如果我是空的(a),那么
@brettdj为什么不发布你自己的答案呢?:)因为你已经抓住了关键部分,我们已经重复了你的答案。为什么要再加一个?:)谢谢,但我只能输入一个数字。@OskarStrasburger这是您最初的问题中没有包含的新信息。
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