+ Reply to Thread
Results 1 to 17 of 17

extracting a specific value from a string

  1. #1
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    extracting a specific value from a string

    Hello,

    I'm trying to keep a portion of the string but since the strings don't have the same length, using text to column cant help me.

    ie.

    A1 = Charlie_Brown_12344567_123C12345678_abcdefg
    change to = 123C12345678

    A1 = John_C_Doe_12344567_123C12345678_123_abcdefg
    change to = 123C12345678_123

    A macro would be much appreciated.
    Last edited by xiao_gosu; 04-24-2010 at 05:18 AM.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: extracting a specific value from a string

    Whether it's done with Formulas or VBA (both are possible), we'd need specific information to tell Excel what specifications you are using to extract? Are there really underscores between each grouping or are there spaces? Are these two examples covering the range of variability one might see in your text?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Quote Originally Posted by ChemistB View Post
    Whether it's done with Formulas or VBA (both are possible), we'd need specific information to tell Excel what specifications you are using to extract? Are there really underscores between each grouping or are there spaces? Are these two examples covering the range of variability one might see in your text?
    Yes, there are underscores in the string and the previous examples cover the variation of each string. I'm trying to extract the "123C12345678" portion of the string (shown at the previous examples where the numbers are random). . I've tried editing Domski's macro for extracting numbers from a string but the result i get contains all the numbers, underscores & the letter "C" in the string.

    examples:

    John_C_Doe_12345678_123C12345678_abcd = _C__12345678_123C12345678_

    Jack_Bauer_12345678_123C12345678_abcd = __12345678_123C12345678_
    the

    the code that I'm using now goes like this:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    ' Keyboard Shortcut: Ctrl+q
    '
    Dim rngLoopRange As Range, i As Long, strResult As String, lngCharacter As Long

    With Selection
    For Each rngLoopRange In Selection
    strResult = ""
    For i = 1 To Len(rngLoopRange)
    lngCharacter = Asc(Mid(rngLoopRange, i, 1))
    If (lngCharacter >= 48 And lngCharacter <= 57) Or (lngCharacter = 67 Or lngCharacter = 95) Then
    strResult = strResult & Mid(rngLoopRange, i, 1)
    End If


    Next i
    rngLoopRange = strResult

    Next rngLoopRange
    End With

    End Sub

    Help :confused

    PS. shoutout to Domski for havin a great macro

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: extracting a specific value from a string

    with some helpers
    Attached Files Attached Files
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: extracting a specific value from a string

    Please take a moment to read the rules and then apply code tags around your code in accordance with rule #3. Thanks.

  6. #6
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Quote Originally Posted by darkyam View Post
    Please take a moment to read the rules and then apply code tags around your code in accordance with rule #3. Thanks.
    My bad. Here's the code

    Please Login or Register  to view this content.
    Thanks to everyone for replying

  7. #7
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Here's some sample data that i'm working with. Thanks in advance!
    Attached Files Attached Files

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: extracting a specific value from a string

    For the sake of demo, based on your sample and expected results, below would be a UDF:

    Please Login or Register  to view this content.
    stored in a Module in VBE this could be called from a cell along the lines of:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Thanks for the response. I inserted the custom code function you showed but when i call the function, it always give me the result "No Term". Any other hints?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: extracting a specific value from a string

    The UDF was based on your sample file - see attached (all results are per your expected values)
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Quote Originally Posted by DonkeyOte View Post
    The UDF was based on your sample file - see attached (all results are per your expected values)
    I was able to make it work after re-downloading the worksheet. Thanks so much for all the help

  12. #12
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    just a follow up. Could someone help me tweak this VBA code so it could give the result i'm looking for? I really want to make it work. It if isn't possible then it's no biggie. Thanks again!

    Please Login or Register  to view this content.

  13. #13
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: extracting a specific value from a string

    That's a pretty vague post... the "result" you're looking for is what exactly ?

    As discussed previously the UDF generated the exact same values as your "expected" results so if they're no longer the desired results it might be an idea to elaborate regards what is.

  14. #14
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: extracting a specific value from a string

    id still go for a function lol
    using given data
    =IF(LEN(MID(C2,FIND("_",C2)+12,255))-LEN(SUBSTITUTE(MID(C2,FIND("_",C2)+12,255),"_",""))=2,LEFT(MID(C2,FIND("_",C2)+12,255),14),LEFT(MID(C2,FIND("_",C2)+12,255),12))

  15. #15
    Registered User
    Join Date
    04-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: extracting a specific value from a string

    Quote Originally Posted by DonkeyOte View Post
    That's a pretty vague post... the "result" you're looking for is what exactly ?

    As discussed previously the UDF generated the exact same values as your "expected" results so if they're no longer the desired results it might be an idea to elaborate regards what is.
    Let me attach a worksheet that uses the macro, that I tried tweaking, to explain it better. Your UDF code really is a big help however, the "result" that I need will be sorted and the UDF won't let you sort the "result" since the "result" will be in formula format: "=stringextract(cellnumber)".
    Attached Files Attached Files

  16. #16
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: extracting a specific value from a string

    the UDF won't let you sort the "result" since the "result" will be in formula format: "=stringextract(cellnumber)".
    Sorry, but can you explain what you mean here? You can absolutely sort the result. Colum E in your latest attachment can be sorted A-Z and Z-A just fine. What's the problem?

  17. #17
    Banned User!
    Join Date
    10-14-2006
    Posts
    1,211

    Re: extracting a specific value from a string

    =SUBSTITUTE(MID(MID(C2,FIND("20",C2),99),FIND("_",MID(C2,FIND("20",C2),99))+1,99),"_"&TRIM(RIGHT(SUBSTITUTE(MID(MID(C2,FIND("20",C2),99),FIND("_",MID(C2,FIND("20",C2),99))+1,99),"_",REPT(" ",99)),99)),"")

    copy down as far as needed

+ 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