Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Find the first number in a string

Posted on 2012-04-10
12
277 Views
Last Modified: 2012-04-10
If the string is "RE: 1 xxxxx" then what is the code that will returne the value 1? Thanks!
0
Comment
Question by:Sasha42
12 Comments
 
LVL 75
ID: 37830025
You can use Val(YourString)

mx
0
 
LVL 75
ID: 37830040
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
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37830092
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

Open in new window

If you're looking for more than a single digit number or potentially more, then we need to do a little more work.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Sasha42
ID: 37830126
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
0
 

Author Comment

by:Sasha42
ID: 37830133
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
0
 
LVL 27

Expert Comment

by:tliotta
ID: 37830212
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
0
 

Author Comment

by:Sasha42
ID: 37830317
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)
0
 

Author Comment

by:Sasha42
ID: 37830322
I tried this too:

If IsNumeric(Mid(varstring, 1, x)) = True Then firstnumber = Mid(varstring, 1, x)
0
 
LVL 75
ID: 37830327
Can you give some additional examples of the strings ?
0
 

Author Comment

by:Sasha42
ID: 37830397
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!
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 37830441
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
0
 

Author Closing Comment

by:Sasha42
ID: 37830456
Perfect! Thanks!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question