Sort by Last Name in Excel When Names Are in One Column
By ExcelReady TeamUpdated 6 min read
The short answer
To sort by last name when full names are in one column, add a helper column that holds just the last name, then sort the table by that column with Data > Sort. In Microsoft 365 or Excel 2024, =TEXTAFTER(A2," ",-1) extracts the last word, and =SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1)) sorts the names in one formula.
Key takeaways
- Excel sorts text from the first letter, so a column of full names sorts by first name.
- A helper column with the last name, then Data > Sort, works in every version.
- =TEXTAFTER(A2," ",-1) returns the last word, even when there is a middle name.
- =SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1)) sorts by last name in one formula in Microsoft 365 or Excel 2024.
- Two-word surnames and suffixes such as Jr. break the last-word rule, so check the results.
On this page+
To sort by last name in Excel when the full name sits in one column, you first need the last name on its own. Add a helper column with a formula such as =TEXTAFTER(A2," ",-1), then sort the table by that column. In Microsoft 365 you can skip the helper column and sort in one formula with SORTBY. This guide shows both ways, plus versions for older Excel and the names that trip up every method.
Why doesn't Excel sort by last name?#
Excel sorts text letter by letter from the left, so "Tom Baker" sorts under T and "Sam Adams" under S. Excel has no idea which part of a name is the surname. You have to give it the last name as a separate value to sort on.
All the examples use this list in A1:A7:
| A | |
|---|---|
| 1 | Name |
| 2 | Priya Shah |
| 3 | Tom Baker |
| 4 | Ana Maria Costa |
| 5 | Leo Wright |
| 6 | Mei Lin |
| 7 | Sam Adams |
Sorted A to Z as it is, the list starts with Ana Maria Costa and ends with Tom Baker. Sorted by last name, it should run Adams, Baker, Costa, Lin, Shah, Wright.
Method 1: a helper column with TEXTAFTER#
A helper column is an extra column that holds a value you only need for sorting or matching. In B1 type "Last name", and in B2 type:
=TEXTAFTER(A2," ",-1)TEXTAFTER returns the text after a delimiter, here a space. The -1 means "count from the end", so it splits at the last space. That makes Ana Maria Costa return Costa, not "Maria Costa". Fill the formula down to row 7.
TEXTAFTER needs Microsoft 365 or Excel 2024. The guide to extracting the last word covers it in more detail.
How to sort the table by the helper column#
With the last names in column B, sort as usual:
- Click any cell in the table.
- Go to Data > Sort (Windows keys: Alt, A, S, S).
- Make sure My data has headers is ticked.
- In Sort by, choose Last name, and in Order, choose A to Z.
- Click OK.
Every row moves together, so other columns such as email or department stay with the right person. Don't select only column A before sorting, or Excel may sort the names and leave everything else behind. If it asks, choose Expand the selection. The guide to sorting data in Excel covers multi-level sorts.
To also sort people with the same surname by first name, click Add Level and pick a first-name column as the second key.
Method 2: sort by last name in one formula with SORTBY#
SORTBY sorts one range by the values in another, and that other range can be calculated. Click an empty cell such as C2 and type:
=SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1))The result spills down six cells: Sam Adams, Tom Baker, Ana Maria Costa, Mei Lin, Priya Shah, Leo Wright. The original list doesn't change, and the sorted copy updates when you edit a name.
To sort a whole table, give SORTBY the full range: =SORTBY(A2:C7,TEXTAFTER(A2:A7," ",-1)). To break ties by first name, add a second key:
=SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1),1,TEXTBEFORE(A2:A7," "),1)SORTBY needs Excel 2021 or Microsoft 365, and TEXTAFTER needs Microsoft 365 or Excel 2024. The comparison of SORT vs SORTBY explains why SORT on its own can't do this: SORT can only sort by a column that is already in the range, which here means using the helper column, as in =SORT(A2:B7,2). The SORT function guide lists its arguments.
How to get the last name in older versions of Excel#
Without TEXTAFTER, use FIND to locate the space. FIND returns the position of one piece of text inside another:
=MID(A2,FIND(" ",A2)+1,100)For "Priya Shah", the space is character 6, so MID returns everything from character 7: Shah. The 100 is just "enough characters to reach the end". The FIND function guide explains the arguments.
This finds the first space, so Ana Maria Costa returns "Maria Costa". When some names have middle names, use this formula instead, which works in every version:
=TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100))It replaces each space with 100 spaces, takes the last 100 characters (the last word plus padding), and TRIM removes the padding. It returns Costa. The guide to splitting a full name shows the matching first-name formulas.
Method 3: Flash Fill, no formula#
Flash Fill copies a pattern you type. In B2, type the first last name, Shah, by hand. Then go to Data > Flash Fill (Windows: Ctrl+E). Excel fills the other last names by following your example.
Flash Fill types plain values, not formulas, so it won't update if a name changes later. Check rows with middle names, because Flash Fill guesses the pattern from your examples. If it gets one wrong, correct that cell and run it again.
Names that break every method#
Splitting at the last space is a rule, and real names don't always follow it. Check the results for:
- Two-word surnames such as "Maria de la Cruz" or "Jan van Dijk". The last word is Cruz or Dijk, which may not be how the person sorts their name.
- Suffixes such as "Jr." or "III", which become the "last name".
- Extra spaces, such as two spaces between names or a space at the end. Clean the column with TRIM first, or the last word may come out blank.
- Names already in "Last, First" order. These sort correctly as they are. To flip them, use
=TEXTAFTER(A2,", ")&" "&TEXTBEFORE(A2,",").
For a large list, the most reliable fix is to store first and last names in separate columns from the start.
Practise it#
Splitting names is a classic job test task. Practise TEXTBEFORE and TEXTAFTER in a live spreadsheet: you type the formula and it checks it instantly. Then practise FIND and SEARCH for the older approach and practise the SORT function to order the results. Or try the SORTBY exercise on this page first.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Sort these names by last name in one formula. Click C2 and type =SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1)), then press Enter.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name | Sorted by last name | ||
| 2 | Priya Shah | |||
| 3 | Tom Baker | |||
| 4 | Ana Maria Costa | |||
| 5 | Leo Wright | |||
| 6 | Mei Lin | |||
| 7 | Sam Adams | |||
| 8 | ||||
| 9 | ||||
| 10 |
Tip: TEXTAFTER with a space and -1 returns the text after the last space: the last name. SORTBY sorts the names by those last names. The list should start with Sam Adams.
Practise this in a lesson
Frequently asked questions
How do I sort by last name in Excel?
Add a helper column with the last name, for example =TEXTAFTER(A2," ",-1), fill it down, then select the table and use Data > Sort to sort by the helper column from A to Z.
How do I sort by last name without a helper column?
In Microsoft 365 or Excel 2024, use =SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1)). It returns the names sorted by their last word in a new range and leaves the original list unchanged.
How do I extract the last name in older versions of Excel?
Use =MID(A2,FIND(" ",A2)+1,100) when every name has exactly two words. For names with middle names, use =TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)), which returns the last word.
How do I sort by last name and then first name?
Add both keys to SORTBY: =SORTBY(A2:A7,TEXTAFTER(A2:A7," ",-1),1,TEXTBEFORE(A2:A7," "),1). With helper columns, add a second level in the Sort dialog for the first-name column.
Can Flash Fill extract last names?
Yes. Type the first person's last name next to their full name, then use Data > Flash Fill (Windows: Ctrl+E). Excel fills the rest by following your pattern, but check rows with middle names.