test to confirm non-numeric characters

  • Thread starter Thread starter Fan924
  • Start date Start date
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
 
Hi

Look at this example:

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


Regards,
Per
 
did you try
If IsNumeric(your_variable) Then
MsgBox "ok"
Else
MsgBox "not ok"
End If
 
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.
 
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
 
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
 
Back
Top