# How to swap first and last names in Google Sheets

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

**Google Sheets:** `=REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1")`

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

## Example

Sample data (row 1 is the header):

| A | B |
| --- | --- |
| Name (Last, First) | Name |
| Lima, Ana |  |
| Patel, Raj |  |
| Chen, Lee |  |
| Van Dyke, Mary Ann |  |
| Ortiz, Kim |  |

Formula in B2, filled to B6: `=REGEXREPLACE(A2,"^(.+), (.+)$","$2 $1")`

Result: Ana Lima; Raj Patel; Lee Chen; Mary Ann Van Dyke; Kim Ortiz

## 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.

## Other ways

- `=TEXTAFTER(A2,", ")&" "&TEXTBEFORE(A2,", ")`: The Excel version.
- `=REGEXREPLACE(A2,"^(\w+), (\w+)$","$2 $1")`: `\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*`.

## 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")))`.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/swap-first-and-last-names
