+ Reply to Thread
Results 1 to 3 of 3

Extracting Numbers from text String and Sum?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2004
    Posts
    16

    Extracting Numbers from text String and Sum?

    From within a single cells of text/numbers I need to extract only those numbers that are preceded by a ~ character and then sum them.
    So as example, one such text formatted cell contains:

    1991 SARW VIN #MG0910433R in 1982 Homemade Semi Trailer VIN #NEBR007742 ~30k; Trailer VIN #CLA1995W248T4112 ~10k; Trailer VIN #1024395 ~8k; Model #LS-4207, VIN #1C9GB42363G864016 ~15k

    Result should be 63 (which was the sum of 30+10+8+15).

    I'm really stuck on this one, so any help would be greatly appreciated!
    FuzzyDove

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    This seems to work :
    Sub Sum_Values()
        Const STRING_TO_SEARCH = "1991 SARW VIN #MG0910433R in 1982 Homemade Semi Trailer VIN #NEBR007742 ~30k; Trailer VIN #CLA1995W248T4112 ~10k; Trailer VIN #1024395 ~8k; Model #LS-4207, VIN #1C9GB42363G864016 ~15k"
        Dim sum As Long, strDigits As String, pos As Integer
        
        strDigits = ""
        sum = 0
        For pos = 1 To Len(STRING_TO_SEARCH)
            If Mid(STRING_TO_SEARCH, pos, 1) = "~" Then
                pos = pos + 1
                Do While Mid(STRING_TO_SEARCH, pos, 1) >= "0" And Mid(STRING_TO_SEARCH, pos, 1) <= "9"
                    strDigits = strDigits & Mid(STRING_TO_SEARCH, pos, 1)
                    pos = pos + 1
                Loop
                sum = sum + Val(strDigits)
                strDigits = ""
            End If
        Next pos
        MsgBox sum
        
    End Sub

  3. #3
    Registered User
    Join Date
    11-19-2004
    Posts
    16
    TJ -
    I cannot thank you enough... your code works perfectly even when I modify it for several variations. The people who use this every day will be so greatful because it will be a huge time saver. Thanks so much for taking the time to help me!! I hope to return the favor some day.
    FuzzyDove

+ 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