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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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
Is this code case sensitive?
Yes it is. That part can be handled by changing it to this:-
InStr(1, IBox, "e",vbTextCompare)
My issue with that method was that 4d4, caused me the same issue as 4e4 and I didn't want to have to try out every letter.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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