VBA: IsNumeric Function Returning True Even If Data Contains Letters. Any Other Way To Validate Entry?

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am using an inputbox to assign value(s) to my variable. After that I use the IsNumeric Function to check if the entry is a valid number.

During the testing process, I realized that I mistakenly entered 4e4 and instead of the function flagging that out as an invalid number, it went through as a number.

I have tried replacing the e with other letters and they were caught by the function yet the e always goes through when it comes between two numbers.

Is there a better way to validate my input?

Thanks in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Are your entries always going to be whole numbers ie are you expecting decimal points or comma ?
@Rick Rothstein has posted a fairly comprehensive treatment in this thread on the forum: vba userform textbox numeric only

You may be able to get away with just this part of what was posted there:
(where strInput is the result of the Input Box)

VBA Code:
    If Not (strInput Like "*[!0-9]*") Then
        MsgBox "Is Numeric " & strInput
    Else
        MsgBox "Is NOT Numeric, try again " & strInput
    End If
 
Upvote 0
4e4 means 4 and four 0 (4000)
Try like this:
PHP:
Sub test()
Dim IBox
IBox = InputBox("Input value:")
    If InStr(1, IBox, "e") = 0 And IsNumeric(IBox) Then
        MsgBox "Valid Number"
    Else
        MsgBox "Invalid Number"
    End If
End Sub
 
Upvote 0
This version worked for me when I tried inserting decimal no.
VBA Code:
If Not (strInput Like "*[!0-9,.]*") And IsNumeric(strInput) Then
Added IsNumeric to handle more than one decimal in the input
 
Upvote 0
Solution
4e4 means 4 and four 0 (4000)
Try like this:
PHP:
Sub test()
Dim IBox
IBox = InputBox("Input value:")
    If InStr(1, IBox, "e") = 0 And IsNumeric(IBox) Then
        MsgBox "Valid Number"
    Else
        MsgBox "Invalid Number"
    End If
End Sub
Is this code case sensitive?
 
Upvote 0
This version worked for me when I tried inserting decimal no.
VBA Code:
If Not (strInput Like "*[!0-9,.]*") And IsNumeric(strInput) Then
Added IsNumeric to handle more than one decimal in the input

Code:
If Not (strInput Like "*[!0-9.]*") And IsNumeric(strInput) Then

This version worked for me since the comma was treating entries like
2,3 as valid numbers.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,470
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top