# Find the first number in a string

If the string is "RE: 1 xxxxx" then what is the code that will returne the value 1? Thanks!
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Database Architect / Systems AnalystCommented:
You can use Val(YourString)

mx
Database Architect / Systems AnalystCommented:
Now, when you say First Number ... val() will return N number of digits at the start of a string ... except ... if the first digit is zero AND there are more digits after that, the zero will get dropped.

Is the number always the first character in your string ?

If so .... the just Left(YourString, 1)

mx
``````varString = "Re: 1 xxx"

For x = 1 to Len(varString)
if IsNumeric(Mid(varString, 1, 1)) = True Then FirstNumber =  Mid(varString, 1, 1)
exit for
Next
``````
If you're looking for more than a single digit number or potentially more, then we need to do a little more work.
Author Commented:
Dim varstring As String
Dim firstnumber As Integer

varstring = "re: 3 xxxxx"
For x = 1 To Len(varstring)
If IsNumeric(Mid(varstring, 1, i)) = True Then firstnumber = Mid(varstring, 1, i)

Exit For
Next
MsgBox (firstnumber)

The above returns a 0
Author Commented:
Dim varstring As String
Dim firstnumber As Integer

varstring = "re: 3 xxxxx"
For x = 1 To Len(varstring)
If IsNumeric(Mid(varstring, 1, 1)) = True Then firstnumber = Mid(varstring, 1,1)

Exit For
Next
MsgBox (firstnumber)

Correction: The above returns a 0
Commented:
Do you want "first number" or "first digit"?

The above returns a 0

In both comments, you're using "For x =...", but you never use 'x' anywhere within the loop. In the first comment, you use 'Mid(varstring, 1, i)'. Try using 'Mid(varstring, x, 1))' instead.

Tom
Author Commented:
I want the first number.  This still returns a zero.  Thanks for your help!

Dim varstring As String
Dim firstnumber As Integer

varstring = "re: 3 xxxxx"
For x = 1 To Len(varstring)
If IsNumeric(Mid(varstring, x, 1)) = True Then firstnumber = Mid(varstring, x, 1)

Exit For
Next
MsgBox (firstnumber)
Author Commented:
I tried this too:

If IsNumeric(Mid(varstring, 1, x)) = True Then firstnumber = Mid(varstring, 1, x)
Database Architect / Systems AnalystCommented:
Can you give some additional examples of the strings ?
Author Commented:
I am pulling data out of the subject line of an Outlook message:

FW: 1 xxxxxxx - Approved
RE:  3 xxxxxxx - Denied
1 xxxxxxx - Approved

I want to pull the first number, 1,3,1

Thanks!
Database Architect / Systems AnalystCommented:
Try this function ... works for all the cases you listed:

Public Function mGetFirstNumber(sStr As Variant) As Variant

''
''  FW: 1 xxxxxxx - Approved
''  RE:  3 xxxxxxx - Denied
''  1 xxxxxxx -Approved
''  abcdefghi9
If IsNull(sStr) Then Exit Function

If IsNumeric(Left(sStr, 1)) Then
mGetFirstNumber = Left(sStr, 1)
Exit Function
End If

Dim iCnt As Long
For iCnt = 1 To Len(sStr)
If IsNumeric(Mid(sStr, iCnt, 1)) Then
mGetFirstNumber = Mid(sStr, iCnt, 1)
Exit Function
End If
Next iCnt

End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Perfect! Thanks!
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.