View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Efficient way to drtermine if a string contains digits?

Actually, this is faster:

Function PositionFirstNumberInString2(strString As String) As Long

Dim i As Long
Dim lPos As Long

For i = 0 To 9
lPos = InStr(1, strString, i, vbBinaryCompare)
If lPos 0 Then
PositionFirstNumberInString2 = lPos
Exit Function
End If
Next i

PositionFirstNumberInString2 = -1

End Function


RBS


"RB Smissaert" wrote in message
...
This might suit you and it is fast:

Function PositionFirstNumberInString(strString As String) As Long

Dim i As Long
Dim btArray() As Byte

btArray = strString

For i = 0 To UBound(btArray) Step 2
If btArray(i) 47 And btArray(i) < 58 Then
PositionFirstNumberInString = i \ 2 + 1
Exit Function
End If
Next

PositionFirstNumberInString = -1

End Function


Use it like this:

If PositionFirstNumberInString("aaaaa2bbbbb3mmmmm") -1 Then
Msgbox "this string has a digit, so answer is True"
End If

You could change the function to a Boolean output if you want, but it
makes sense
to keep the position information.


RBS


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!!