+ Reply to Thread
Results 1 to 10 of 10

Extract String After Specific Word

  1. #1
    Registered User
    Join Date
    11-28-2011
    Location
    Singapore, Singapore
    MS-Off Ver
    Excel 2007
    Posts
    19

    Extract String After Specific Word

    Hi,

    I have a report of 20000+ rows. I need to extract certain data from one of the column. For the example below, I need to extract the 9 digits that comes after 'SN#'.

    May I know if this is possible, given the complexity of the data..

    [6 rows under same column]:
    [R1]Record - DN No. 98765421 SO# 1343434 for CUSROMER 1 - SN# - 07/29/2011'
    [R2]Record - DN No. 12345677 SO# 1222222 for CUSTOMER 2 - SN# - 07/29/
    [R3]Hardness Test Results SN# - SN# 133449982-1, PO , HEAT# - 07/29/2011'
    [R4]Inspection for Main Part SN# - SN# 123456098-1 TO 4, PO , HEAT# - 07/29/2011'
    [R5]Dimensional for Main Part SN# - SN# 119221033, PO# HN# - 07/29/2011'
    [R6]Dimensional for Sub Part SN# - SN# 120012999-1 TO 5, PO# HN# - 07/29/2011'

    I should be getting in another column:
    [R1]
    [R2]
    [R3] 133449982
    [R4] 123456098
    [R5] 119221033
    [R6] 120012999
    Last edited by Gingeiko; 12-03-2011 at 09:12 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Extract String After Specific Word

    UDF
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-28-2011
    Location
    Singapore, Singapore
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Extract String After Specific Word

    Thanks Nilem, for your quick reply.

    I am still very new to VBA. So far, I've only been using Sub()...End Sub. So, I'm not sure if I am using this right.

    Based on the example from my first post, the data is from A1 to A6.

    In B1, i placed the formula =SN(A1), and fill to B6. But what I get in B1 to B6 is:
    #NAME?
    #VALUE!
    #NAME?
    #NAME?
    #NAME?
    #NAME?
    Last edited by Gingeiko; 12-02-2011 at 03:16 AM.

  4. #4
    Registered User
    Join Date
    11-28-2011
    Location
    Singapore, Singapore
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Extract String After Specific Word

    Ok I have no idea why, but after switching between windows, when i returned to my worksheet, it is working! Amazing!

    THANKS NILEM!

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Extract String After Specific Word

    Hi Gingeiko
    or if excel function is prefered

    =LEFT(B3,4)&IF(ISNUMBER(SEARCH("SN# - SN# ",B3)),MID(B3,SEARCH("SN# - SN# ",B3)+10,9),"")
    Last edited by pike; 12-02-2011 at 04:26 AM. Reason: added LEFT(B3,4)&
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Registered User
    Join Date
    11-28-2011
    Location
    Singapore, Singapore
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Extract String After Specific Word

    Thanks Pike for the reply.

    However I think the example I gave has some flaw. Thing is, the number i need to extract doesn't always come with SN# - SN# before it, sometimes it's just SN# followed by 9 digits.

    So am I right to say in this case, regular expression is still a better way of doing it? I mean it does exactly what I need, detecting a pattern of SN# \d{9}, and return the 9 digits.

    But I do have a question, is it possible to incorporate the user-defined function, as provided by Nilem, into my Sub instead? I have tried to do some research and I believe I should do something as follow:

    Please Login or Register  to view this content.
    I am not sure what comes after that, if I need to search the string in column D, and return the 9 digits in column E.

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

    Re: Extract String After Specific Word

    You could certainly persist with VBA & RegExp but I still suspect a formula driven approach would suffice.

    Based on your last comment to Pike perhaps:

    Please Login or Register  to view this content.
    where A1 holds string

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Extract String After Specific Word

    or this ia all i can up with

    =LEFT(B3,4)&IF(ISNUMBER(SEARCH(" SN# ?????????-",B3)),MID(B3,SEARCH(" SN# ?????????-",B3)+4,9),IF(ISNUMBER(SEARCH(" SN# ?????????,",B3)),MID(B3,SEARCH(" SN# ?????????,",B3)+4,9),""))

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Extract String After Specific Word

    or
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-28-2011
    Location
    Singapore, Singapore
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Extract String After Specific Word

    WOW...thanks Donkeyote, Pike and Nilem for your quick replies.

    I'm pretty new to macros, so all this looks really impressive to me, both Formulas and RegExp. and... I would need some time to digest them! Thanks a lot all of you!

+ 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