Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting numbers from a string 2

Status
Not open for further replies.

GstRdr1

MIS
May 29, 2002
17
US
I need to extract the numbers from a string and store them in a variable. For example I have thestring "05hdmj09kj03" how would I get vb to recognize just the 050903? I know that it's probably easy but I'm still new at this. Thanks

Marc
MCP -WinXP
-Win2k Server
 
If you don't know how many numbers there are or where they are, you can do a simple loop:

Dim s As String
Dim sNum As String
Dim i As Integer

s = "05hdmj09kj03"
sNum = ""

For i = 1 To Len(s)
If IsNumeric(Mid(s, i, 1)) Then sNum = sNum & Mid(s, i, 1)
Next
Debug.Print sNum
 
If the result can contain leading zeros it will have to be assigned to a string variable.

Function fNumbersFromString(ByVal strIn As String) As String

Dim lngC1 As Long

If strIn = vbNullString Then Exit Function

For lngC1 = 1 To Len(strIn)
If IsNumeric(Mid$(strIn, lngC1, 1) Then
fNumbersFromString = fNumbersFromString & _
Mid$(strIn, lngC1, 1)
End If
Next

End Function

Then call it as follows:

Dim strNumbers As String
strNumbers = fNumbersFromString("05hdmj09kj03")

Paul Bent
Northwind IT Systems
 
Thanks for you help!

Marc
MCP -WinXP
-Win2k Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top