View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Efficient way to drtermine if a string contains digits?

I like the first approach. The 2nd one is not very flexible, say if you
wanted to add a period or other misc chars.


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can use this function instead....

Function HasCharAllowed(ByVal S As String) As Boolean
Const CharsAllowed = "0123456789"
HasCharAllowed = S Like "*[" & CharsAllowed & "]*"
End Function

And, if you want, you can even shorten the Const statement to this...

Const CharsAllowed = "0-9"

Other characters can be added to the allowed character list as long as the
"special" ones described in the help files for the Like operator are
handled as mentioned there.

Rick


Another way, a reusable function that enables you to modify to the chars
allowed in the text, now limited to numbers:

Sub Test2()
MsgBox HasCharAllowed("3 test")
End Sub

Function HasCharAllowed(ByVal s As String) As Boolean
Const CharsAllowed = "0123456789"
Dim i As Integer
HasCharAllowed = False
For i = 1 To Len(s)
If InStr(CharsAllowed, Mid(s, i, 1)) 0 Then
HasCharAllowed = True
Exit For
End If
Next
End Function




wrote in message
...
What is the most efficient way to determine if any of the characters of
a
string are digits?

One example would be a function that returns "TRUE" if one or more
characters of its single argument are digits and "FALSE" otherwise.

Thanks!!