+ Reply to Thread
Results 1 to 6 of 6

Extract multiple numbers from a text string???

Hybrid View

abz Extract multiple numbers from... 06-08-2010, 05:45 PM
rylo Re: Extract multiple numbers... 06-08-2010, 06:36 PM
abz Re: Extract multiple numbers... 06-08-2010, 07:05 PM
abz Re: Extract multiple numbers... 06-08-2010, 07:10 PM
foxguy Re: Extract multiple numbers... 06-08-2010, 07:11 PM
shg Re: Extract multiple numbers... 06-08-2010, 09:26 PM
  1. #1
    Registered User
    Join Date
    06-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    41

    Extract multiple numbers from a text string???

    Hi there, basically i have a text string with text and multiple numbers at different character positions in a cell. I need to extract these numbers only, doesnt really matter in what format as long as i get ALL the numbers in another cell. A sample of a typical cell is below:



    363 - which station poster232 - cab tube179 - inter car barrier188 - control key replaced893 CAB LIGHT TUBE340 LIGHT TUBE111 LIGHT TUBE

    i want all the numbers within this text string.

    I have previously used the following custom formula to extract the numbers but that only works to extract the first 15 numbers and then for the remaining numbers it just returns 0's, although they are the exact amount of zeros as are the exact amount of numbers left in the string.
    Function ExtractNumber(rCell As Range, _
         Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double
     
    Dim iCount As Integer, i As Integer, iLoop As Integer
     
        Dim sText As String, strNeg As String, strDec As String
     
        Dim lNum As String
     
        Dim vVal, vVal2
     
        sText = rCell
     
        If Take_decimal = True And Take_negative = True Then
     
            strNeg = "-"
     
            strDec = "."
     
        ElseIf Take_decimal = True And Take_negative = False Then
     
            strNeg = vbNullString
     
            strDec = "."
     
        ElseIf Take_decimal = False And Take_negative = True Then
     
            strNeg = "-"
     
            strDec = vbNullString
     
        End If
     
        iLoop = Len(sText)
     
                For iCount = iLoop To 1 Step -1
     
                vVal = Mid(sText, iCount, 1)
     
                    If IsNumeric(vVal) Or vVal = strNeg Or vVal = strDec Then
     
                        i = i + 1
     
                        lNum = Mid(sText, iCount, 1) & lNum
     
                            If IsNumeric(lNum) Then
     
                                If CDbl(lNum) < 0 Then Exit For
     
                            Else
     
                              lNum = Replace(lNum, Left(lNum, 1), "", , 1)
     
                            End If
     
                    End If
     
                    If i = 1 And lNum <> vbNullString Then lNum = CDbl(Mid(lNum, 1, 1))
     
                Next iCount
     
        ExtractNumber = CDbl(lNum)
     
    End Function

    The result that i get with this formula is 363232179188893000000

    this is not what i want, i want all the numbers.


    Anyone any help please?? what am i doing wrong here?

    Thanks
    Last edited by abz; 06-08-2010 at 07:15 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Extract multiple numbers from a text string???

    Hi

    Can you please review the forum rules regarging tags around code. I've edited your post for you this time.

    The truncation is the result of the double number type. If you convert the type to String and change the last line to be
    ExtractNumber = lNum
    then it will output all the numbers.

    HTH

    rylo

  3. #3
    Registered User
    Join Date
    06-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Extract multiple numbers from a text string???

    Hi Rylo

    ill take care in my next posts.

    Thanks for that

    Tbh with you i am not an expert on this vba code, i just got it off the internet and theres a few bits in this that i dont need, anyway the question is how do i convert the double type to string?

    Thanks

  4. #4
    Registered User
    Join Date
    06-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Extract multiple numbers from a text string???

    Rylo,

    Dun worry about my last message,just figure it out, it was the first line i had to look through, i wasnt doing that in the first place. Thanks for that it works now.

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Extract multiple numbers from a text string???

    Change the function to return a string instead of a double
    Function ExtractNumber(rCell As Range, _
         Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As String
         ......
        ExtractNumber = lNum
    End Function
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Extract multiple numbers from a text string???

    Another way:
    Function GetNums(sInp As String) As String
        Dim i As Long
        
        For i = 1 To Len(sInp)
            Select Case Mid(sInp, i, 1)
                Case "0" To "9"
                    GetNums = GetNums & Mid(sInp, i, 1)
            End Select
        Next i
    End Function
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1