Spreadsheet Practice

How to swap first and last names in Google Sheets

Google Sheets formula

=REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1")

Capture both parts and put them back in the other order: =REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1") turns "Lima, Ana" into "Ana Lima".

Result on the example
one result per row in B2, filled down to B6
Excel
See below

See it work

The formula is already in B2, filled down to B6. Change any input and the result updates, or edit the formula to experiment.

How it works

The pattern captures everything before the comma as group 1 and everything after the comma and space as group 2. In the replacement, $2 $1 writes them back in the other order with a space between. Using .+ instead of \w+ keeps multi-word parts like "Van Dyke" and "Mary Ann" together.

=REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1")

Result"Ana Lima"

  1. A2"Lima, Ana"

    The value in A2.

  2. REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1")"Ana Lima"

    REGEXREPLACE: Replaces every match. Use $1 for a captured group.

    • textA2"Lima, Ana"
    • regular_expression"^(.+), (.+)$"
    • replacement"$2 $1"

In Excel

In Excel 2024 or Microsoft 365, rebuild the name with TEXTAFTER and TEXTBEFORE: =TEXTAFTER(A2,", ")&" "&TEXTBEFORE(A2,", ").

Other ways to do it

Each one was run on the same example sheet.

FormulaResultWhen to use it
=TEXTAFTER(A2,", ")&" "&TEXTBEFORE(A2,", ")Excel onlyAna Lima, Raj Patel, Lee Chen…The Excel version.
=REGEXREPLACE(A2,"^(\w+), (\w+)$","$2 $1")Ana Lima, Raj Patel, Lee Chen…Different result\w+ only matches single words, so two-word names are left unchanged. Van Dyke, Mary Ann stays as it is.

Common mistakes

  • Names without a comma pass through unchanged, which is usually what you want, but check for them.
  • Extra spaces after the comma end up at the start of the result. Wrap A2 in TRIM, or allow them with ,\s*.

Try it yourself

Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.

Swap first and last names

In B2, turn the "Last, First" name in A2 into "First Last". Fill down to B6.

Questions

How do I go the other way, "First Last" to "Last, First"?

Split at the last space: =REGEXREPLACE(A2,"^(.+) (\S+)$","$2, $1").

How do I do a whole column at once?

Wrap it: =ARRAYFORMULA(IF(A2:A="","",REGEXREPLACE(A2:A,"^(.+), (.+)$","$2 $1"))).

Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine.