[Solved] How to find the last word in cell, copy to new cell

Discuss the spreadsheet application
Post Reply
3graygirls
Posts: 14
Joined: Wed Jun 19, 2019 2:49 am

[Solved] How to find the last word in cell, copy to new cell

Post by 3graygirls »

I have cells that contain peoples names, each word separated by a space. How do I find the last word (name) in a cell and place it in a separate column? There may be only one name.

Examples:

Mary Ann Doolittle
Peter O'Toole
John Wayne
Ralph G. Cramden
Madonna
I. B. Daft
J. Dee Bull
Juan Francisco Henry Pound

Text to column won't work as I want only one new column with just the last name. If there is only one name, e.g. Madonna, that should go in the "last name" column.

CGH
Last edited by 3graygirls on Tue Jul 09, 2019 2:32 am, edited 1 time in total.
LibreOffice Calc on Windows 7
Version: 6.2.4.2 (x64)
User avatar
robleyd
Moderator
Posts: 5082
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How do I find the last word in cell and copy to a new c

Post by robleyd »

[Solved] Extracting right part of text leads to a solution for selecting text after the final space in a string.

Combine that with a search for space(s) in the string; if no string is found, display the entire string; otherwise use the formula from ^^link above^^ to extract the text after the space.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: How do I find the last word in cell and copy to a new c

Post by RusselB »

A modification of Ken Johnson's code

Code: Select all

=FIND(CHAR(0);SUBSTITUTE(A1;"\";CHAR(0);LEN(A1)-LEN(SUBSTITUTE(A1;"\";""))))
from my topic at viewtopic.php?f=9&t=74250 would work.
The modification I'd suggest would be to make it

Code: Select all

=MID(A1;IF(ISERR(FIND(CHAR(0);SUBSTITUTE(A1;" ";CHAR(0);LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))));0;FIND(CHAR(0);SUBSTITUTE(A1;" ";CHAR(0);LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))))+1;LEN(A1))
I realize this is a fairly long code, and if you want to break it down, you'll need to use helper columns. I basically used helper columns to get the code to work, then combined everything into one.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
3graygirls
Posts: 14
Joined: Wed Jun 19, 2019 2:49 am

Re: How do I find the last word in cell and copy to a new c

Post by 3graygirls »

Thank you, RusselB. It worked great on 176 rows. Thank you both for responding quickly. I would never have gotten there.
CGH
LibreOffice Calc on Windows 7
Version: 6.2.4.2 (x64)
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: [Solved]How to find the last word in cell, copy to new

Post by RusselB »

I'm guessing that you stopped at 176 because that was all the data you had to work with, rather than the formula not working for some.
Please note that, per your request, that presumes the person's surname (aka last name) is at the end, and not followed by any additional information like Jr., Sr., III
Also note that it will return a technically incorrect name for those races that put last name first, like the Chinese and Japanese.
You'll have to handle those situations manually on a one by one basis, as I don't know of any way to write a code to handle those exceptions... or at least, not in Calc.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
3graygirls
Posts: 14
Joined: Wed Jun 19, 2019 2:49 am

Re: [Solved]How to find the last word in cell, copy to new

Post by 3graygirls »

Thank you for the caveat. Asian names will be rare, if ever, in this genealogy project, and they'll appear however my source has them. I'll be on the lookout for the Jr., etc. and handle them manually. Pointing out both of these situations is helpful.
CGH
LibreOffice Calc on Windows 7
Version: 6.2.4.2 (x64)
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved]How to find the last word in cell, copy to new

Post by FJCC »

I am a little late to this party. Here is a somewhat simpler formula

Code: Select all

=MID(A1;SEARCH("[^\s]+$";A1);LEN(A1))
More caveats about last names. Lots of cultures have multi word family names. In Spanish speaking countries, everyone has two family names. Gabriel Garcia Marquez. In Dutch - Vincent Willem van Gogh. In German - Johann Wolfgang von Goethe. Names are a nighmare!
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply