test to confirm non-numeric characters

F

Fan924

I have a 10 character number string. I need a test to confirm that
there are no non-numeric characters in this string. Thanks
Excel97
 
P

Per Jessen

Hi

Look at this example:

If isnumeric(MyString) then
'numeric string
else
'whatever
endif


Regards,
Per
 
G

Gary Keramidas

did you try
If IsNumeric(your_variable) Then
MsgBox "ok"
Else
MsgBox "not ok"
End If
 
R

Rick Rothstein

Consider this previous posting of mine...

I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note below):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.
 
R

Rick Rothstein

In other words, you are looking for a test to see if your 10 character
number string is made up of digits only, right? Here is a function that will
do that...

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And _
Not Value Like "*[!0-9]*"
End Function

If you want to incorporate this function's code directly within your own
code (without needing to call a separate function), then your test would be
structured something like this...

If Len(TenCharString) > 0 And Not TenCharString Like "*[!0-9]*" Then
' TenCharString is made up of digits only
Else
' At least one character in TenCharString is not a digit
End If
 
R

Rick Rothstein

Here is the function without making use of the distracting line
continuation..

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And Not Value Like "*[!0-9]*"
End Function

--
Rick (MVP - Excel)


Rick Rothstein said:
In other words, you are looking for a test to see if your 10 character
number string is made up of digits only, right? Here is a function that
will do that...

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Len(Value) > 0 And _
Not Value Like "*[!0-9]*"
End Function

If you want to incorporate this function's code directly within your own
code (without needing to call a separate function), then your test would
be structured something like this...

If Len(TenCharString) > 0 And Not TenCharString Like "*[!0-9]*" Then
' TenCharString is made up of digits only
Else
' At least one character in TenCharString is not a digit
End If

--
Rick (MVP - Excel)


Fan924 said:
I have a 10 character number string. I need a test to confirm that
there are no non-numeric characters in this string. Thanks
Excel97
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top