Finding number in string

radek225

Registered User.
Local time
Today, 13:01
Joined
Apr 4, 2013
Messages
307
Have a strings like this
Code:
dsa;hwq;67;dk;71c
Code:
uqiea;762c;iyh

Is there any possibilites to write a function which find number in string, even if some part (between ";" and ";") has number and text (like 762c)?

if there is number in string then function is true
 
You might need a UDF to get you this information. There is no native function that can do this. Something like,
Code:
Public Function HasNumbers(InputStr As String) As Boolean
    Dim tmpArr() As String, lCtr As Integer
    
    tmpArr = Split(InputStr, ";")
    
    For lCtr = 0 To UBound(tmpArr)
        If Val(tmpArr(lCtr)) <> 0 Then
            HasNumbers = True
            Exit Function
        End If
    Next
End Function
So,
Code:
?? HasNumbers("uqiea;762c;iyh")
True
 
As usual, perfectly. THX!
 
You're welcome ! Although there is a small disadvantage to this, if the string happens to be c762c. The function will fail. As Val will yield the Numeric value left to a String. There is an alternative using a IsNumeric looping through literally every single character and seeing if a number appears. Would you be interested in that?
 
hmm No it's not necessary. There's no way to have something like this (c762c). But now I see another problem. Forgot to say about that, (Sorry!). If in string will be "Pant." then function also should be true. Is it possible to achieve it?
String like
Code:
asd;xga;Pant. jas;ueq
 
Of Course you can, something like.
Code:
Public Function HasNumbers(InputStr As String) As Boolean
    Dim tmpArr() As String, lCtr As Integer
    
    tmpArr = Split(InputStr, ";")
    
    For lCtr = 0 To UBound(tmpArr)
        If Val(tmpArr(lCtr)) <> 0 [B][COLOR=Blue]Or InStr(tmpArr(lCtr), "Pant") <> 0[/COLOR][/B] Then
            HasNumbers = True
            Exit Function
        End If
    Next
End Function
 

Users who are viewing this thread

Back
Top Bottom