View Full Version : Does VBA string contain number?

07-04-2008, 11:39 AM
I'm trying to modify a vba script I wrote, and need to check whether a particular user defined string contains a number (the string is an address)

so if user types in "5 Jones Street", I'd get containsnumber = 1
and if user types in "Jones Street", I'd get containsnumber = 0

or something along those lines. The first character isn't always a number (eg "A-E/5 Jones Street") - the number can be anywhere in the string.

Can anyone point me in the right direction? I am guessing I could use something like the search and mid functions (like in Excel), but how I tell it to specifically look for a number in the string I don't really know.

This is not an Excel vba script, so using specifically Excel functions may not work.

Does all that make sense? :)


07-04-2008, 11:55 AM
I've done it, although if anyone has a better/correct way to do it I'd appreciate you posting it here :)

Dim myString As String
Dim i As Integer
Dim myOtherString As String
myString = "A-J 5 Jones Street"
For i = 0 To 9
If InStr(1, myString, i) > 0 Then
myOtherString = "Blah"
End If
Next i
If myOtherString <> "Blah" Then
MsgBox "no numbers found"
MsgBox "numbers found"
End If