+ Reply to Thread
Results 1 to 16 of 16

Extract String After Second Occurrence of Delimiter

Hybrid View

  1. #1
    Registered User
    Join Date
    08-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    17

    Extract String After Second Occurrence of Delimiter

    Gurus,

    I have a column of full names, and each entry in the column could look like one of these:

    lastname, firstname middlename
    lastname, firstname
    lastname, firstname middleinitial
    hyphenated-lastname, firstname
    hyphenated-lastname, firstname, middleinitial
    hyphenated-lastname, firstname, middle name
    firstname lastname
    firstname middleinitial lastname
    firstname middlename lastname
    firstname secondname middlename lastname
    lastname suffix, firstname middlename
    lastname suffix, firstname
    lastname suffix, firstname middleinitial
    hyphenated-lastname suffix, firstname
    hyphenated-lastname suffix, firstname, middleinitial
    hyphenated-lastname suffix, firstname, middle name
    firstname lastname suffix
    firstname middleinitial lastname suffix
    firstname middlename lastname suffix
    firstname secondname middlename lastname suffix

    I know; my client has a problem standardizing their data...

    So, what I need, at the end of the day, is firstname secondname in the original column, and lastname suffix in the next column. All of the middlenames or middleinitials can be stripped.

    I envision selecting the name column cells (they vary in number) and running a VBA macro that I've added to the command ribbon.

    Super appreciate any help!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Extract String After Second Occurrence of Delimiter

    Would you clarify that this is indeed a single column of text items separated by spaces?

    Is the Suffix always EITHER the second string of text OR the last string?
    Do you have a list of suffixes that could be used to check against?
    Is it safe to assume that any non suffix text string of a maximum of say two characters will always be a middle initial(s)?

    Can you upload a file with some real data examples. The solution may not need a macro.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Extract String After Second Occurrence of Delimiter

    With data mixed up like that, how would excel know what a first name is and what a last name (or any name, really), is?

    We see this kind of question fairly often here.
    Is Stanely a 1st name - is it a last name - could it be a middle name?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    08-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Extract String After Second Occurrence of Delimiter

    If it is a comma, it is last, first. No comma, it is first last. There are rules, I can parse them every time - just was hoping to automate it.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Extract String After Second Occurrence of Delimiter

    Quote Originally Posted by pwyller View Post
    If it is a comma, it is last, first. No comma, it is first last. There are rules, I can parse them every time - just was hoping to automate it.
    But that's not entirely true. With No Comma there is also first middle initial, or first name second name etc..

    If you're now saying there are rules which allow you to parse them manually, then don't you think it might be a good idea to state those rules and show us how they have been applied to an example set of actual data - as I asked for in post #2?

  6. #6
    Registered User
    Join Date
    08-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Extract String After Second Occurrence of Delimiter

    Sorry, you missed the point. With a comma, the last name is always first (with or without suffix), followed by the first name and initial or another name. Without a comma, it is always first name first, middle or second name(s), and last name (with or without suffix). The examples I provided are all inclusive and define the rule sets.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Extract String After Second Occurrence of Delimiter

    Quote Originally Posted by pwyller View Post
    Sorry, you missed the point. With a comma, the last name is always first (with or without suffix), followed by the first name and initial or another name. Without a comma, it is always first name first, middle or second name(s), and last name (with or without suffix). The examples I provided are all inclusive and define the rule sets.
    Hi,

    But that explanation only deals with three name parts (first name first, middle or second name(s), and last name) plus a suffix if applicable. In your data you also have firstname secondname middlename lastname suffix, i.e. four name elements plus suffix.

    We often find that when we give an answer to what is a trivial non representative example, when the solution is used with the real world data it doesn't work because of factors like string length and other stuff which is important but hasn't been mentioned.

    Therefore upload your real workbook (or at least a cut down copy) as requestes, and manually add the results you expect to see.

  8. #8
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Extract String After Second Occurrence of Delimiter

    Can you provide a complete list of suffixes?
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  9. #9
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Extract String After Second Occurrence of Delimiter

    Here's a formula solution (a bit of a monster, though)...
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Extract String After Second Occurrence of Delimiter

    You need to refine sfix or list all suffixes in range.
    Sub test()
        Dim a, i As Long, sfix As String
        sfix = "sr.|jr.|phd|etc"
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            a = .Value
            With CreateObject("VBScript.RegExp")
                .Global = True: .IgnoreCase = True
                .Pattern = "([$^()\\{}\[\]+*?.-])"
                sfix = "(" & .Replace(sfix, "\$1") & ")"
                For i = 1 To UBound(a, 1)
                    .Pattern = "^([^, -]+-)?([^, .-]+) " & sfix & ", (\S+).*"
                    If .test(a(i, 1)) Then
                        a(i, 1) = .Replace(a(i, 1), "$4 $2")
                    Else
                        .Pattern = "^(\S+).* (\S+) " & sfix & "$"
                        If .test(a(i, 1)) Then
                            a(i, 1) = .Replace(a(i, 1), "$1 $2")
                        Else
                            .Pattern = "^([^, -]+-)?([^, ]+), (\S+).*"
                            If .test(a(i, 1)) Then
                                a(i, 1) = .Replace(a(i, 1), "$3 $2")
                            Else
                                .Pattern = "^(\S+).* (\S+)$"
                                If .test(a(i, 1)) Then
                                    a(i, 1) = .Replace(a(i, 1), "$1 $2")
                                End If
                            End If
                        End If
                    End If
                Next
            End With
            .Columns("b") = a
        End With
    End Sub

  11. #11
    Registered User
    Join Date
    08-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Extract String After Second Occurrence of Delimiter

    Let's refine; anything left of a comma is a last name, anything to the right of a comma (to the last space in the string) is the first name, anything after a second space can be truncated.

    If there is no comma, everything to the left of the first space is the first name (unless it is MR, MRS, or MS - those can be truncated), anything to the right of the last space is the last name - unless it is one of the following: JR, SR, I, II, III - then it is part of the last name.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Extract String After Second Occurrence of Delimiter

    Post a workbook with possible different examples and the result that you want.

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Extract String After Second Occurrence of Delimiter

    Quote Originally Posted by jindon View Post
    Post a workbook with possible different examples and the result that you want.
    ..and please make sure it contains real world examples...

  14. #14
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Extract String After Second Occurrence of Delimiter

    Pwyller, as a matter of interest, did you try the formula solution offered at post #9?

  15. #15
    Registered User
    Join Date
    08-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Extract String After Second Occurrence of Delimiter

    Yes, those are similar to the formulas I'm currently using, but I'm trying to build a VBA macro that I can put in the command ribbon so I can off-load this task to a clerk -- one step instead of many (with the ever-present problem of basic Excel-user tendencies to break formulas).

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Extract String After Second Occurrence of Delimiter

    I don't think that any logic can do a complete job of parsing names. If human decision making ("is this a last name or a first") isn't used when entering the data, it will have to be used after a macro (which gets most of the names right) is applied.

    Let me offer this tool for that final clean up. Double click on a cell and its contents will be swapped with the cell on the right.Private Sub
    ' in a sheet's code module
    
    Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim temp As Variant
        With Target
            Cancel = True
            temp = .Value
            .Value = .Offset(0, 1).Value
            .Offset(0, 1).Value = temp
            .Offset(0, 1).Select
        End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Extracting text from string between delimiter X & Y
    By dchubbock in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-12-2016, 07:14 AM
  2. VBA Code to Extract Text Between Delimiter/Characters
    By jadown in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2016, 12:27 PM
  3. Replies: 12
    Last Post: 08-14-2014, 11:37 AM
  4. [SOLVED] Extract the first characters inside a cell up to a delimiter
    By pickslides in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-24-2014, 08:50 PM
  5. [SOLVED] truncate a string x characters after a delimiter
    By timmatthews in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-19-2013, 05:56 PM
  6. Replies: 6
    Last Post: 03-27-2012, 06:00 AM
  7. Edit text string using delimiter?
    By 508 in forum Excel General
    Replies: 3
    Last Post: 03-14-2009, 06:03 PM

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