簡體   English   中英

VBA Excel,輸入框不匹配為整數

[英]VBA Excel, mismatch for inputbox as 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

如果我留下一個空字段,Excel將返回Type Mismatch錯誤。 我想顯示一條消息。

由於aInteger ,因此它不能包含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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM