How to split first and last names in Excel

A guest list stores one full name in a cell. The mailing column wants the given name. The place card wants the last word. Those are different cuts, and a middle name is what makes them different.

The sample names are Maria Lopez, Maria Elena Lopez, Madonna, and Lopez, Maria. The spaced cell is the one on the TRIM page: two spaces, then Maria, two spaces, Lopez, two spaces.

Trim before you look for the space

TRIM of Maria Lopez is Maria Lopez. The spaces it removes are ordinary spaces, character 32. TEXTBEFORE of the trimmed cell, with a space as the delimiter, is Maria. TEXTAFTER of the same cell is Lopez. Instance −1, the last space, is also Lopez, because there is only one space left.

TEXTBEFORE
=TEXTBEFORE(B3," ")
=TEXTBEFORE(B3," ")B4
Trim, then split
AB
1Value
2Raw Maria Lopez
3CleanMaria Lopez
4GivenMaria
5RestLopez
6Last wordLopez
7Given, untrimmed
A picture of the TRIM page's name. B2 has two leading spaces, two spaces between the words, and two trailing spaces. TRIM returns Maria Lopez. TEXTBEFORE of the trimmed cell is Maria. TEXTBEFORE of the raw cell is blank, because the first character is already a space.

TEXTBEFORE of the raw cell is blank. The first character is a space, so the text before the first space is empty. TEXTAFTER of the raw cell starts with a space and still has the double gap. Split the trimmed cell. The TRIM page also says a non-breaking space, character 160, survives TRIM. A name copied from a web page can look clean and still fail an exact match against Maria Lopez. SUBSTITUTE character 160 with a normal space, then TRIM, which is the fix on that page.

TEXTBEFORE and TEXTAFTER need Microsoft 365 or Excel 2024, including Excel for the web. Excel 2021 returns #NAME?.

Four names, two cuts

Maria Lopez is one space. The given name, the rest, and the last word are Maria, Lopez, and Lopez.

Maria Elena Lopez is two spaces. The first space is after Maria. The rest is Elena Lopez, middle name included. The last space is before Lopez, so the last word is Lopez. LEN of Maria Elena Lopez is 17. The first space is character 6. The last space is character 12.

Madonna has no space. TEXTBEFORE and TEXTAFTER return #N/A, which is the missing-delimiter result on those two function pages. The last-word formula wraps TEXTAFTER in IFERROR and returns Madonna. A guest with one name stays one name.

Lopez, Maria is already last-name first. TEXTBEFORE returns Lopez,, comma included, because the comma is not the delimiter. The last word is Maria, which is the given name. xl.last.word says the same thing: the last word is the wrong guess for Lopez, Maria.

TEXTAFTER
=IFERROR(TEXTAFTER(TRIM(A2)," ",-1),TRIM(A2))
=IFERROR(TEXTAFTER(TRIM(A3)," ",-1),TRIM(A3))E3
Four names
ABCDE
1NameGivenGiven, 2010RestLast word
2Maria LopezMariaMariaLopezLopez
3Maria Elena LopezMariaMariaElena LopezLopez
4Madonna#N/AMadonna#N/AMadonna
5Lopez, MariaLopez,Lopez,MariaMaria
6Count of Lopez12
A picture of four guest names. Maria Elena Lopez has Maria in the given column and Elena Lopez in the rest. The last word is Lopez. Madonna has no space, so TEXTBEFORE and TEXTAFTER are #N/A and the 2010 formula returns Madonna. The count row is Lopez: 1 in the rest column and 2 in the last-word column.

COUNTIF of the last-word column for Lopez is 2. Maria Lopez and Maria Elena Lopez both end in Lopez. COUNTIF of the rest column for Lopez is 1. Elena Lopez is not Lopez. A place-card count that uses the rest column undercounts the family name as soon as anyone has a middle name.

The same cuts without TEXTBEFORE

FIND of a space returns the position. LEFT takes everything before it. MID takes everything after it. FIND of a missing space is #VALUE!, so ISERROR has to catch that before LEFT runs. These functions are in Excel 2010.

=IF(ISERROR(FIND(" ",TRIM(A2))),TRIM(A2),LEFT(TRIM(A2),FIND(" ",TRIM(A2))-1))
=IF(ISERROR(FIND(" ",TRIM(A2))),"",MID(TRIM(A2),FIND(" ",TRIM(A2))+1,LEN(TRIM(A2))))
=TRIM(RIGHT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",100)),100))

The first formula is the given name. On Maria Elena Lopez, FIND returns 6, LEFT takes 5 characters, and the result is Maria. On Madonna, FIND errors and the formula returns Madonna.

The second formula is the rest. MID starts one character after the space. LEN is longer than the tail, and MID stops at the end of the text, so the result is Elena Lopez. On Madonna the result is an empty string, not #N/A.

The third formula is the last word, and it does not need TEXTBEFORE. SUBSTITUTE turns every space into 100 spaces. RIGHT of 100 characters is then the last word, padded, and TRIM removes the pad. Maria Elena Lopez returns Lopez. Madonna returns Madonna, because there is no space to expand. Lopez, Maria returns Maria.

Maria Lopez Jr. is the suffix case on the last word page. The last word is Jr. The family name is not the last word once a suffix is still in the cell. Remove the suffix before the cut if the column is supposed to be Lopez.

The named formulas

xl.split.name spills two cells. TRIM runs first. The first cell is the given name. The second is the rest, middle name included.

=xl.split.name("Maria Lopez")
=xl.split.name("Maria Elena Lopez")
=xl.split.name("  Maria  Lopez  ")
=xl.split.name("Madonna")

The results are Maria and Lopez, then Maria and Elena Lopez, then Maria and Lopez again, then Madonna and a blank second cell. The name does not reorder Lopez, Maria. That call spills Lopez, and Maria. xl.initials returns the uppercase first letter of each word, so Maria Elena Lopez becomes MEL.

xl.first.word is only the given name, and it returns the whole cell when there is no space. xl.last.word is the last word, with the same fallback. =xl.last.word("Maria Elena Lopez") is Lopez. =xl.first.word("Maria Elena Lopez") is Maria.

All three names need Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME?. The FIND formulas above are the same cuts without them. Saving a name is the same Name Manager step as the LAMBDA how-to.

The guest list these columns belong on is the wedding guest list. The clean name is Maria Lopez, and the last word Lopez appears twice.


Template for this article

Wedding Guest List

More from the blog