+ Reply to Thread
Results 1 to 25 of 25

Extracting last name from a string

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Extracting last name from a string

    Hi,

    I have names entered into a cell per the following:

    John Q Smith

    Bob Allen Jones

    William B Howard


    How do I extract in another cell the last name using a function?

  2. #2
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Extracting last name from a string

    Assuming the last name is always the last word.
    Enter in B1 and copy down
    Formula: copy to clipboard
    =TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",50)),50))

    v A B
    1 John Q Smith
    Smith
    2 Bob Allen Jones Jones
    3 William B Howard Howard
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  3. #3
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    Thank you! That worked just fine.

    I also found out one of the files has names listed like this:

    Smith, Brian L
    Jones, Evan R
    Gray, David S "Buddy"

    How do I extract the first name in to a separate cell using a function/formula?

    (Using the "Mid" function I can get to the correct starting position but don't know how to get it to compute the right length)
    Last edited by rmichra; 05-16-2018 at 09:48 PM.

  4. #4
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    I need to extract the first name

  5. #5
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Extracting last name from a string

    Just for fun
    Done with PowerQuery:
    Attached Files Attached Files

  6. #6
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Extracting last name from a string

    First name from:

    John Q Smith
    Bob Allen Jones
    William B Howard
    Smith, Brian L
    Jones, Evan R
    Gray, David S "Buddy"

    done with PowerQuery
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    Can this be done using a combination of functions?

  8. #8
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Extracting last name from a string

    Probably yes but I prefer make life easier not harder

    maybe someone else will give you formula solution

    have a nice day

  9. #9
    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: Extracting last name from a string

    if there will ALWAYS be a comma for that format and NOT in the other format, then try this...
    =IFERROR(LEFT(B14,SEARCH(",",B14)-1),TRIM(RIGHT(SUBSTITUTE(B14," ",REPT(" ",50)),50)))
    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

  10. #10
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    The name will always have a comma.

    I used the formula and it extracted the last name. I need the name following the comma.

  11. #11
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Extracting last name from a string

    Hi Ford,
    What about list from post#6?

  12. #12
    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: Extracting last name from a string

    If there is always a comma when the last name appears 1st - and NO comma when the last name is last, that will handle them both
    B
    C
    14
    Smith, Brian L Smith
    15
    Jones, Evan R Jones
    16
    Gray, David S "Buddy" Gray
    17
    John Q Smith Smith
    18
    Bob Allen Jones Jones
    19
    William B Howard Howard

  13. #13
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Extracting last name from a string

    I was thinking about result like this:

    John
    Bob
    William
    Brian
    Evan
    David

    First name
    D
    E
    1
    RAW Extracted
    2
    John Q Smith John
    3
    Bob Allen Jones Bob
    4
    William B Howard William
    5
    Smith, Brian L Brian
    6
    Jones, Evan R Evan
    7
    Gray, David S "Buddy" David


    I'm curious. I need to write monster formula to extract middle part (first name) from text with comma than extract first part from text without comma all with IF or you know something shorter
    Last edited by sandy666; 05-16-2018 at 10:55 PM.

  14. #14
    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: Extracting last name from a string

    OK, I missed that you wanted the 1st name, if there is a comma, I will work something out

  15. #15
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    Thank you!

    Here are 2 examples how the names can be listed:

    Smith, Billy R
    Smith, Billy A "Ben"

    I need to extract the first name.

  16. #16
    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: Extracting last name from a string

    Try this...
    =IF(ISERROR(FIND(",",B14)),TRIM(RIGHT(SUBSTITUTE(B14," ",REPT(" ",50)),50)),LEFT(MID(B14,LEN(LEFT(B14,SEARCH(" ",B14,1)-1))+2,99),FIND(" ",MID(B14,LEN(LEFT(B14,SEARCH(" ",B14,1)-1))+2,99))-1))
    B
    C
    14
    Smith, Brian L Brian
    15
    Jones, Evan R Evan
    16
    Gray, David S "Buddy" David
    17
    John Q Smith Smith
    18
    Bob Allen Jones Jones
    19
    William B Howard Howard

  17. #17
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    Thank You.

  18. #18
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Extracting last name from a string

    or maybe this one:

    A
    B
    C
    1
    RAW Result Formula
    2
    John Q Smith John =IFERROR(MID(A2,SEARCH(",",A2)+2,SEARCH(" ",MID(A2,SEARCH(",",A2)+2,99))-1),LEFT(A2,SEARCH(" ",A2)-1))
    3
    Bob Allen Jones Bob
    4
    William B Howard William
    5
    Smith, Brian L Brian
    6
    Jones, Evan R Evan
    7
    Gray, David S "Buddy" David

  19. #19
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Extracting last name from a string

    Try this one
    Formula: copy to clipboard
    =TRIM(MID(SUBSTITUTE(IF(ISERR(FIND(",",A2))," "&A2,A2)," ",REPT(" ",50)),50,50))

    v A B
    1 Name
    2 John Q Smith John
    3 Bob Allen Jones Bob
    4 William B Howard William
    5 Smith, Brian L Brian
    6 Jones, Evan R Evan
    7 Gray, David S "Buddy" David
    8 Smith, Billy R Billy
    9 Smith, Billy A "Ben" Billy
    Last edited by AlKey; 05-17-2018 at 05:03 PM.

  20. #20
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    That works perfect. Thank you. :-)

  21. #21
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    Hello again,

    I was using your formula and it was working great. However, I got an erroneous result when I encountered a name like:
    Smith Jr., Billy A "Ben"

    The formula returns the value "Jr.".

    I tried messing with the formula to get it to work but no avail.

    Your help would be greatly appreciated.

  22. #22
    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: Extracting last name from a string

    Quote Originally Posted by rmichra View Post
    ...
    I was using your formula and it was working great....
    Who's formula did you use?

  23. #23
    Registered User
    Join Date
    09-21-2017
    Location
    Fort Worth, Texas
    MS-Off Ver
    2010
    Posts
    20

    Re: Extracting last name from a string

    =trim(mid(substitute(if(iserr(find(",",a2))," "&a2,a2)," ",rept(" ",50)),50,50))

  24. #24
    Valued Forum Contributor jtakw's Avatar
    Join Date
    05-05-2018
    Location
    CA, USA
    MS-Off Ver
    2016
    Posts
    668

    Re: Extracting last name from a string

    Hi,

    If there's Always a Space after the comma use B2 formula, otherwise use C2 formula:

    Excel 2016 (Windows) 64 bit
    A
    B
    C
    1
    Name
    2
    John Q Smith John John
    3
    Bob Allen Jones Bob Bob
    4
    William B Howard William William
    5
    Smith, Brian L Brian Brian
    6
    Jones, Evan R Evan Evan
    7
    Gray, David S "Buddy" David David
    8
    Smith, Billy R Billy Billy
    9
    Smith, Billy A "Ben" Billy Billy
    10
    Smith Jr., Billy A "Ben" Billy Billy
    11
    Smith Jr.,Billy A "Ben" illy Billy
    Sheet: Sheet44

    Excel 2016 (Windows) 64 bit
    B
    C
    2
    =TRIM(LEFT(SUBSTITUTE(MID(A2,IFERROR(FIND(",",A2)+2,1),255)," ",REPT(" ",100)),100)) =TRIM(LEFT(SUBSTITUTE(TRIM(MID(A2,IFERROR(FIND(",",A2)+1,1),255))," ",REPT(" ",100)),100))
    Sheet: Sheet44

  25. #25
    Valued Forum Contributor jtakw's Avatar
    Join Date
    05-05-2018
    Location
    CA, USA
    MS-Off Ver
    2016
    Posts
    668

    Re: Extracting last name from a string

    Have you tried the formulas in my Post #22?

+ 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 value from the string
    By ks1102 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2014, 11:45 PM
  2. Extracting UK postcodes from a string
    By Ian99099 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 01-29-2014, 07:06 AM
  3. [SOLVED] Extracting from a string
    By DavidNO in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-24-2013, 05:34 AM
  4. Replies: 12
    Last Post: 03-20-2013, 05:46 PM
  5. Extracting a number from a string
    By SgtSunshine in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-25-2010, 07:33 AM
  6. Extracting 28 from a string
    By ephie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2006, 12:17 PM
  7. [SOLVED] Extracting a string
    By Peter Rooney in forum Excel General
    Replies: 5
    Last Post: 06-20-2006, 01:40 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