30 Advanced Excel Tips for 2026

Excel Champs / Excel Formulas List / Flip the First & Last Names and Add a Comma Between
By Puneet Gogia (Microsoft MVP)

Flip the First & Last Names and Add a Comma Between

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

To turn “John Smith” into “Smith, John”, the trick is to join the name to itself and then take a slice from the middle: =MID(A1&", "&A1,SEARCH(" ",A1)+1,LEN(A1)+1). Doubling the text means the last name and the first name end up next to each other across the join, so a single MID starting just after the space picks up both in the right order.

In Excel 365 there’s a far more readable version: =TEXTAFTER(A1," ")&", "&TEXTBEFORE(A1," "). The classic two-part version works in any release: =MID(A1,SEARCH(" ",A1)+1,LEN(A1))&", "&LEFT(A1,SEARCH(" ",A1)-1).

Two limits to know. A cell holding a single word returns #VALUE!, because SEARCH can’t find a space — wrap it in =IFERROR(...,A1) to leave those unchanged.

And a middle name goes to the wrong side: “John Paul Smith” becomes “Paul Smith, John”, since the formula splits at the first space. For a one-off conversion with no formula, type the flipped version next to the first row and press Ctrl+E for Flash Fill.

To flip the names (First and Last) into last and first names in Excel, you need to use a combination of MID, LEN, and SEARCH functions. In this tutorial, we will learn to write a formula for this.

flip-first-and-last-name

Excel Formula to Swap First and Last Names

You can use the following steps to write this formula:

  1. First, you need to use the SEARCH function to find the space that you have between both first and last names.
    formula-to-swap-first-and-last-name
  2. After that, add one to the number returned by the SEARCH.
    add-one-to-search-function
  3. Next, you need to use the LEN function to get the count of total characters (first and last name), and add 1 to it as well.
    len-function-to-get-count
  4. Now, enter the MID function, and in the first argument, enter the A1 cell (where you have the name) twice by combining it with a comma and space.
    mid-function-in-first-argument
  5. From here, in the second and third arguments of the MID, enter the SEARCH function and LEN function (which we have written in the earlier steps) respectively.
    enter-search-in-second-and-third-argument-of-mid
  6. In the end, hit Enter to get the result.
    enter-to-get-result
=MID(A1&", "&A1,SEARCH(" ",A1)+1,LEN(A1)+1)

For “John Smith” in cell A1, this formula returns “Smith, John”.

How Does This Formula Work

To understand this formula, you need to split it into three parts. The main function is the MID function, and we have three arguments in the MID function to define, and that’s why you need to split this formula into three parts to understand it.

how-mid-and-search-formula-worked

In the FIRST PART, we have the text argument of the MID. And in this argument, we have combined the first and the last names twice using a comma and a space.

first-part-has-text-argument

In the SECOND PART, you have the SEARCH function that refers to cell A1, where you have the names (first and last), looks for the space in it, and then adds 1 to it. This way, you get the starting position of the last name from the cell.

second-part-has-search-function

In the THIRD PART, you have the LEN function that counts the total number of characters from the original names (first and last) and adds 1 to it.

third-part-has-len-function

In the end, the MID function gets the last name from the first value and the first name from the second value, which gives you the last name and the first name with a comma in between. The extra 1 in the length makes room for the comma and the space, which are one character more than the space it skips.

Names with Extra Spaces

If a name has a space at the start or two spaces between the names, the formula returns a broken result. Wrap the cell in TRIM to clean the spaces first:

=MID(TRIM(A1)&", "&TRIM(A1),SEARCH(" ",TRIM(A1))+1,LEN(TRIM(A1))+1)

Names with a Middle Name

The formula above uses the first space, so for “John Paul Smith”, it returns “Paul Smith, John”. To use the last word as the last name, you need to find the last space instead. In Microsoft 365, use TEXTAFTER and TEXTBEFORE with -1, which count from the end:

=TEXTAFTER(A1," ",-1)&", "&TEXTBEFORE(A1," ",-1)

In older versions, this formula finds the last space with SUBSTITUTE and returns “Smith, John Paul”:

=MID(A1,FIND("#",SUBSTITUTE(A1," ","#",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))+1,100)&", "&LEFT(A1,FIND("#",SUBSTITUTE(A1," ","#",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))-1)

Flip Names Back or without a Comma

To turn “Smith, John” back into “John Smith”, use the same idea, but look for the comma and the space:

=MID(A1&" "&A1,SEARCH(", ",A1)+2,LEN(A1)-1)

And if you only want to swap the names with a space and no comma (“Smith John”), use this one:

=MID(A1&" "&A1,SEARCH(" ",A1)+1,LEN(A1))

You can also use Flash Fill: type the first result by hand in the next column, select the cell below it and press Ctrl + E. Flash Fill fills in the rest by following your example, but it doesn’t update when the names change.

Frequently Asked Questions

Why does the formula return #VALUE!?

SEARCH can’t find a space, so the cell has only one name or is empty. Wrap the formula in IFERROR, like =IFERROR(MID(A1&”, “&A1,SEARCH(” “,A1)+1,LEN(A1)+1),A1), to return the name as it is.

Can I replace the names in the same column?

Yes. Write the formula in a helper column, copy the results and use Paste Special ➜ Values over the original names, then delete the helper column.

Does it matter if I use FIND instead of SEARCH?

Not here. FIND is case-sensitive and SEARCH isn’t, but a space has no case, so both find the same position.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel

Leave a Comment