String Analysis

  • Thread starter Thread starter Peter Weinwurm Jr
  • Start date Start date
P

Peter Weinwurm Jr

Hello

I am looking for a function that can tell me if there is a character (non
numerical Character) in a string or whether the string totally consists of
numbers.

Hence if I call the function on the string "123455" it would return false
(or True based on different logic that still applies) because there are no
non-numerical characters.


Thanks alot
 
Hi, Peter. I know of no built-in function, but you can
easily create one:

Function StringIsAllNumbers(strTestString As String) _
As Boolean
Dim intAscValue As Integer

For i = 1 To Len(strTestString)
intAscValue = Asc(Mid(strTestString, i, 1))
If (intAscValue < 48 Or intAscValue > 57) Then
StringIsAllNumbers = False
Exit Function
End If
Next

StringIsAllNumbers = True
End Function

HTH
Kevin Sprinkel
 
I am looking for a function that can tell me if there is a character (non
numerical Character) in a string or whether the string totally consists of
numbers.

IsNumeric comes close: 123.4 and 3.2E5 will return True because they
are valid numbers.

If you want to more narrowly define numeric as consisting of just
digits and no other characters, you'll probably need a small custom
VBA function: off the top of my head -

Public Function AllNum(strIn As String) As Boolean
Dim iPos As Integer
For iPos = 1 to Len(strIn)
If Not IsNumeric(Mid(strIn, iPos, 1) Then
AllNum = False
Exit Function
End If
Next iPos
AllNum = True
End Function
 
Peter Weinwurm Jr said:
Hello

I am looking for a function that can tell me if there is a character
(non numerical Character) in a string or whether the string totally
consists of numbers.

Hence if I call the function on the string "123455" it would return
false (or True based on different logic that still applies) because
there are no non-numerical characters.

I think that you can test whether string X has a non-numeric character
in by using the Like operator:

If X Like "*[!0-9]*" Then
' X has a non-numeric character
Else
' X is all digits or is a zero-length string.
End If
 
Back
Top