ExcelReady
CourseMock testsFunction guidesBlog
Start freeStart learning for free
ExcelReady

Be Excel-ready before day one. Hands-on practice in your browser, with instant feedback.

Learn

  • Course
  • Mock tests
  • Function guides
  • Blog

Test prep

  • Excel level test
  • Excel test practice
  • Free screening quiz
  • Certificate

Company

  • About
  • Editorial policy
  • Contact
  • Terms
  • Privacy

© 2026 ExcelReady. All rights reserved.

  1. Blog
  2. Text
  3. Sort by Last Name in Excel When Names Are in One Column

Sort by Last Name in Excel When Names Are in One Column

By ExcelReady Team·Updated November 2, 2026·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+
  1. Why doesn't Excel sort by last name?
  2. Method 1: a helper column with TEXTAFTER
  3. How to sort the table by the helper column
  4. Method 2: sort by last name in one formula with SORTBY
  5. How to get the last name in older versions of Excel
  6. Method 3: Flash Fill, no formula
  7. Names that break every method
  8. Practise it

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
1Name
2Priya Shah
3Tom Baker
4Ana Maria Costa
5Leo Wright
6Mei Lin
7Sam 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:

Excel formula
=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:

  1. Click any cell in the table.
  2. Go to Data > Sort (Windows keys: Alt, A, S, S).
  3. Make sure My data has headers is ticked.
  4. In Sort by, choose Last name, and in Order, choose A to Z.
  5. 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:

Excel formula
=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:

Excel formula
=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:

Excel formula
=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:

Excel formula
=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.

fx
ABCD
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

LessonTEXTBEFORE and TEXTAFTERSplit emails, "Last, First" names and file paths at a delimiter with TEXTBEFORE and TEXTAFTER, including picking which occurrence to split at.LessonSORT: order data with a formulaSort a table by any column, ascending or descending, and combine SORT with FILTER and UNIQUE.LessonFIND and SEARCHLocate a character or word inside text with FIND or SEARCH, then use that position to split names and pull out order numbers.Function guideSORTSort a range with a formula using Excel's SORT. Sort by any column, highest first, or combined with FILTER. Includes a live exercise.Function guideFINDLocate a character inside text with Excel's FIND, then use it with LEFT, MID and RIGHT. Case rules, #VALUE! fixes and a live exercise.

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.

Sources

  • SORTBY function - Microsoft Support
  • TEXTAFTER function - Microsoft Support
  • Sort data in a range or table - Microsoft Support

Written and checked by the ExcelReady Team. First published November 2, 2026. The practice exercise runs in the same formula engine as our lessons. Found a mistake? Read our editorial policy or email hello@excelready.app.

On this page

  1. Why doesn't Excel sort by last name?
  2. Method 1: a helper column with TEXTAFTER
  3. How to sort the table by the helper column
  4. Method 2: sort by last name in one formula with SORTBY
  5. How to get the last name in older versions of Excel
  6. Method 3: Flash Fill, no formula
  7. Names that break every method
  8. Practise it

Keep reading

Text· 7 min read

How to Split First and Last Names in Excel

To split a full name in Excel, use =TEXTBEFORE(A2," ") for the first name and =TEXTAFTER(A2," ",-1) for the last name, which also handles middle names. In Excel 2021 and older, use =LEFT(A2,FIND(" ",A2)-1) for the first name. For a one-off job, Data > Text to Columns or Flash Fill splits names without formulas.

Data tools· 6 min read

How to Sort Data in Excel by One or More Columns

To sort data in Excel by one column, click any cell in that column and choose Data > Sort A to Z or Sort Z to A. To sort by several columns, choose Data > Sort, pick the first column, then click Add Level for the next one. Click a single cell first so Excel keeps each row together.

Text· 7 min read

Extract the Last Word From a Cell in Excel

To extract the last word from a cell in Microsoft 365 or Excel 2024, use =TEXTAFTER(A2," ",-1). The -1 tells Excel to start from the last space instead of the first. In older versions, use =TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)), which works for any number of words.

More on text

  • How to Split an Address Into Columns in Excel
  • Standardize Inconsistent Data in Excel: One Spelling Each
  • SUBSTITUTE and REPLACE in Excel
  • The TEXT Function: Format Numbers and Dates Inside Text
  • TEXTBEFORE and TEXTAFTER in Excel
  • TEXTJOIN in Excel: Join a List and Skip Blanks
All text articles →

Don't just read about Excel. Do it.

Practise real formulas in a live spreadsheet with instant feedback, right in your browser. Nothing to download.

Start learning for free