VBA Excel, mismatch for inputbox as integer

calendar_today Asked Feb 11, 2013
thumb_up 9 upvotes
history Updated April 16, 2026

Question posted 2013 · +8 upvotes

My code

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

If I leave an empty field , Excel returns a Type Mismatch error. I would like to display a message.

Accepted answer +9 upvotes

Since a is an Integer, it cannot contain a String or be Empty. Use a Variant and then check to see what has been returned:

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

Top excel-vba Q&A (6)

+9 upvotes ranks this answer #73 out of 136 excel-vba solutions on this site .