How to Sort by Last Name in Excel
In this article
To sort by last name in Excel, you first need to get the last name into its own column. The quickest way is to use Data > Text to Columns or a formula to split full names, then sort the new Last Name column with Data > Sort. If your full names are formatted as "Last, First", you can often sort directly without splitting.
What You'll Need
- Excel 2016 or later (Windows or Mac) — the steps below work in both, though menu labels can differ slightly by version and region.
- A column containing full names (e.g., "Jane Doe") or a column already in "Last, First" format.
- An empty column to the right of your data for the extracted last names.
Step-by-Step
- Make a backup copy of your worksheet (File > Save As, or press Ctrl+C on the sheet tab and paste a copy). Sorting is reversible with Undo, but splitting names is not always clean, so a backup saves headaches.
- Check that your data is in a proper table with headers. Select any cell in the range and press Ctrl+T (or Cmd+T on Mac) to convert it to an Excel Table. This makes sorting safer because Excel treats the header row separately.
- Insert a new column for last names. Right-click the column letter to the right of your Full Name column and choose Insert. Give it a header like "Last Name".
- Extract the last name using a formula. In the first data row of the new column, enter a formula that pulls everything after the last space. For a full name in cell A2, use:
=TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100))
This handles middle names and extra spaces. Press Enter, then drag the fill handle down to apply it to all rows. If you prefer a simpler formula and your names have exactly two parts,=RIGHT(A2,LEN(A2)-FIND(" ",A2))works. - Convert the formulas to values (optional but recommended). Select the new Last Name column, press Ctrl+C, then right-click and choose Paste Special > Values. This prevents formulas from breaking if you later delete the original name column.
- Select the entire data range (including headers). Click any cell in your table and press Ctrl+A, or select the whole table. If you used an Excel Table, just click inside it.
- Open the Sort dialog. Go to Data > Sort. In the dialog, ensure "My data has headers" is checked. Under "Sort by", choose your new "Last Name" column. Set "Sort on" to Values and "Order" to A to Z (or Z to A). Click OK.
- Verify the results. Check that entire rows moved together — first names should stay with their last names. If only the last name column sorted and other columns stayed put, you likely selected only one column; undo (Ctrl+Z) and repeat with the full range selected.
- Clean up (optional). You can hide the helper Last Name column or delete it after sorting. If you delete it, keep the original full name column intact.
Common Mistakes
- Sorting only one column. If you select just the Last Name column and click Sort A to Z, Excel may ask to expand the selection — always choose "Expand the selection" so entire rows stay together.
- Forgetting headers. If "My data has headers" is unchecked, Excel will sort the header row into your data. Always confirm this box.
- Names with suffixes or multiple spaces. Formulas like the one above handle most cases, but "John Smith Jr." will sort under "Jr." You may need to clean suffixes manually or use a more complex formula.
- Using Text to Columns without checking delimiters. If names contain commas ("Doe, Jane"), Text to Columns with comma delimiter will split correctly, but if some names lack commas, results will be inconsistent. Preview before finishing.
- Not converting formulas to values before deleting source data. If you delete the Full Name column while the Last Name column still contains formulas referencing it, you'll get #REF! errors.
Tips
- If your names are already in "Last, First" format, you can sort directly on that column — Excel sorts by the first character, so "Doe, Jane" will sort under D. No splitting needed.
- Use Data > Text to Columns with Space as the delimiter for a quick split when names have a consistent structure (First Middle Last). You can then delete the middle name column if not needed.
- For recurring tasks, consider using Power Query (Data > Get & Transform) to split names automatically each time you refresh.
- Always work on a copy of your data when experimenting with formulas or splitting, especially if the sheet is shared.
Troubleshooting
- Sort didn't move entire rows: Undo immediately (Ctrl+Z). Then select the entire data range (including all columns) before opening the Sort dialog. If using an Excel Table, click any cell inside it and use Data > Sort — Excel will include all columns automatically.
- Formulas return errors (#VALUE!, #NAME?): Check that the cell references in the formula point to the correct column and row. Also ensure you're using the correct delimiter (space vs. comma) for your data. If your names contain non-breaking spaces, TRIM may not remove them; use
=TRIM(SUBSTITUTE(A2,CHAR(160)," "))first. - Last names include middle names or suffixes: The formula extracts everything after the last space, so "Mary Jane Smith" becomes "Smith" (good), but "John Smith Jr." becomes "Jr." You can manually correct these or use a more advanced formula that looks for common suffixes.
- Sort order looks wrong (e.g., lowercase after uppercase): Excel's default sort is case-insensitive, but if you see unexpected order, check that the column is formatted as Text, not Number. Also ensure no leading spaces exist — use TRIM.
- Excel asks "Sort anything that looks like a number..." This appears when sorting mixed data types. Click "Sort anything that looks like a number, as a number" only if appropriate; otherwise choose "Sort numbers and numbers stored as text separately" to avoid unexpected reordering.
Frequently Asked Questions
Can I sort by last name without adding a helper column?
Yes, if your names are formatted as "Last, First" (e.g., "Doe, Jane"). Excel will sort alphabetically by the first character, which is the last name. If names are "First Last", you need a helper column or Text to Columns.
What if some names have middle initials?
The formula =TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)) extracts the last word after the final space, so "John Q. Public" becomes "Public". Middle initials are ignored, which is usually what you want.
Will sorting by last name break my formulas elsewhere?
Sorting moves entire rows, so relative references within the same row adjust automatically. However, absolute references or cross-sheet links may break if they point to specific cells. Always test on a copy first.
How do I sort by last name in Excel Online?
The steps are nearly identical: select your data, go to Data > Sort, choose the Last Name column, and click OK. Excel Online supports the same Sort dialog, though some advanced options may be limited.
Can I sort by last name and then by first name?
Yes. In the Sort dialog, click "Add Level" and set the second level to your First Name column. This sorts by last name first, then first name within each last name group.
Why does my sort put names with apostrophes or hyphens in odd places?
Excel ignores apostrophes and hyphens for sorting purposes, so "O'Connor" sorts as "OConnor". If you need exact character-level sorting, you may need to use a custom sort order or a formula that strips punctuation.