0

I am trying to do an input validation (Only Accept Numeric Value), however the code below does not seem to work properly. Can someone please shed a light.

Upon entering non-numerical Value, the MsgBox is somehow skipped and the sub routine goes straight to Error:

Sub Check()
Dim A As Integer

On Error GoTo Error
Do
    A = InputBox("Enter A Numerical Value:")
    If IsNumeric(A) Then
        Exit Do
    Else
        MsgBox ("Please Enter A Numerical Value!")
    End If
Loop
Exit Sub

Error:
MsgBox "An Error Has Occured!"
End Sub
6
  • 2
    Why not use Application.InputBox and specify the Type? Commented Jun 3, 2020 at 16:35
  • 2
    Dim A As String - Then use IsNumeric Commented Jun 3, 2020 at 16:36
  • @BigBen Thanks, this is my first VBA project. I didn't know such feature exists. This is a big help! Commented Jun 3, 2020 at 16:44
  • @braX I tried changing A as String and it worked. However I'm still curious to know why Integer doesn't? Commented Jun 3, 2020 at 16:44
  • 4
    If the user enters a string then that will trigger a "type mismatch" runtime error when you try to assign it to A (because you declared A as Integer and that can't hold a string)- so your code goes directly to the error handler. Commented Jun 3, 2020 at 16:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.