View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default Finding a string of 7 numbers in cell contents

Have compared Like "#######" with IsNumeric etc. and depending on the
supplied value Like can be a lot slower,
I would say about 5 times slower:

Option Explicit
Private lStartTime As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Sub test()

Dim i As Long
Dim v
Dim b As Boolean

'v = 1234567
'v = "1234567"
v = Cells(1)

StartSW
For i = 0 To 100000
b = IsDigitsOnly(v)
Next i
StopSW

MsgBox b

End Sub

Function IsDigitsOnly(v As Variant) As Boolean

IsDigitsOnly = v Like String(Len(v), "#")

End Function

Function IsDigitsOnly2(v As Variant) As Boolean

If IsNumeric(v) Then
If CLng(v) = v Then
IsDigitsOnly2 = True
End If
End If

End Function

Sub StartSW()
lStartTime = timeGetTime()
End Sub

Function StopSW(Optional bMsgBox As Boolean = True, _
Optional vMessage As Variant, _
Optional lMinimumTimeToShow As Long = -1) As Variant

Dim lTime As Long

lTime = timeGetTime() - lStartTime

If lTime lMinimumTimeToShow Then
If IsMissing(vMessage) Then
StopSW = lTime
Else
StopSW = lTime & " - " & vMessage
End If
End If

If bMsgBox Then
If lTime lMinimumTimeToShow Then
MsgBox "Done in " & lTime & " msecs", , vMessage
End If
End If

End Function


But if the test value is supplied like this: "1234567" then Like is faster.
Probably speed doesn't come into it for the OP, so I take Like will be best.


RBS



"Rick Rothstein (MVP - VB)" wrote in
message ...
Yes, you are right there. Have tested and all your examples passed the
test.
In that case Like "#######" may be the best test. Not sure how fast it
is and that could be an issue.


I'm not sure how Like compares (no pun intended<g) to IsNumeric
speedwise; but I know it is slower than InStr, so it should not be use in
place of that when testing for substring inclusion within a larger text
string.


How would you test if the number of figures was variable?


This will test a number string to see if it is composed of all digits, no
matter how many digits (literally, no matter how many digits; well, up to
2 billion or so digits at least)...

If NumberString Like String(Len(NumberString), "#") Then


Rick