How to Sort Formulas in Excel: Step-by-Step Guide
In this article
Excel sorts formulas by their results, not by the formula text itself. To sort a column of formulas, select the whole data range (including the formula column and any adjacent columns), then use Data > Sort and choose the column that contains the formulas as the Sort by key. Excel recalculates each formula for its new row position and reorders the rows so the results appear in the order you chose. If you want to sort by the formula text (for example, all formulas starting with SUM first), you cannot do that directly with Excel's Sort dialog; you need a helper column that converts each formula to text, then sort on that helper column.
What You'll Need
- Excel for Microsoft 365, Excel 2021, 2019, or 2016 on Windows or Mac (the steps below use the desktop ribbon; Excel for the web has the same Data > Sort commands but fewer options).
- A worksheet with formulas in at least one column and, ideally, a header row so Excel can detect column names.
- A backup copy of the workbook before you sort, because sorting moves entire rows and can break formulas that reference specific cells.
Step-by-Step
- Save a copy first. Use File > Save As and give the workbook a new name, or press Ctrl+Z immediately after sorting if something goes wrong. Sorting rearranges whole rows, so a formula that pointed to A2 may now point to A7 and return a different value.
- Check whether your formulas use relative or absolute references. Click a formula cell and look at the formula bar. References like A2 are relative and will follow the row when sorted; references like $A$2 are absolute and will keep pointing at the same cell. If your formulas must keep pointing at fixed cells, edit them to use $ before sorting.
- Select the entire data range. Click the top-left cell of your data and drag to the bottom-right, or press Ctrl+A (Cmd+A on Mac) inside the data. Include every column that belongs to each row, not just the formula column, otherwise the rows will no longer line up.
- Open the Sort dialog. Go to Data > Sort. A dialog opens with dropdowns for Column, Sort On, and Order. Menu labels can differ slightly by Excel version and region.
- Tick "My data has headers" if your first row contains column names. This keeps the header row at the top instead of sorting it into the data.
- Choose the formula column in the Sort by dropdown. Pick the column whose formula results you want to order. Under Sort On, leave it as Cell Values (not Cell Color or Font Color) so Excel sorts by the calculated result.
- Pick the order. Choose A to Z or Z to A for text, Smallest to Largest or Largest to Smallest for numbers. Click OK. Excel recalculates every formula and places the rows in the new order.
- Verify the results. Click a few formula cells and confirm the formula bar shows the formula you expect for that row. If a formula now references the wrong cell, undo with Ctrl+Z and fix the references before trying again.
- To sort by formula text instead of results, add a helper column. In an empty column beside your data, enter =FORMULATEXT(A2) (replace A2 with your first formula cell) and fill down. This returns each formula as a text string. Sort the whole range by that helper column, then delete the helper column when finished. FORMULATEXT is available in Excel 2013 and later; in older versions you would need to retype the formulas as text manually.
- If you only want to reorder the formulas and not the surrounding data, copy the formula column, paste it as Values into a temporary column, sort that column, then use it as a reference to rebuild the order. Sorting a single column on its own will break the row alignment, so avoid doing that unless the column is truly independent.
Common Mistakes
- Sorting only the formula column. If you select just column C and click Sort A to Z, Excel asks whether to expand the selection. Choosing "Continue with the current selection" shuffles the formulas away from their rows, and every result becomes wrong. Always expand the selection or select the whole range first.
- Forgetting that relative references move with the row. A formula like =B2*C2 is fine after sorting because it still multiplies the same row's B and C. A formula like =$B$2*C2 will keep pulling B2 even after the row moves, which is usually not what you want.
- Sorting a filtered list. Excel sorts only the visible rows when a filter is active in some versions, leaving hidden rows in place and producing a mixed order. Clear filters with Data > Clear before sorting.
- Sorting merged cells. Merged cells cause the error "This operation requires the merged cells to be identically sized." Unmerge them first (Home > Merge & Center), sort, then remerge if needed.
- Assuming the formula text is sortable. The Sort dialog sorts by calculated value, not by the formula string. Without a helper column, there is no built-in way to order by formula text.
Tips
- Use a helper column with =FORMULATEXT() whenever you need to group formulas by type, such as all VLOOKUP formulas together.
- Convert formulas to values (Copy, then Paste Special > Values) before sorting if you want the results to stay frozen and not recalculate.
- Press Ctrl+Z right after a sort if anything looks wrong. Sorting is a single undoable action in desktop Excel.
- Use absolute references ($A$1) for any cell that must stay fixed through a sort, such as a tax rate or a lookup table on another sheet.
- If you sort often, turn your range into an Excel Table (Ctrl+T). Tables keep rows together and make sorting safer.
Troubleshooting
- Formulas show #REF! after sorting. A formula referenced a cell that moved out of range. Undo, check the references, and switch to absolute references where needed.
- Results do not match the new order. You likely sorted only part of the range. Undo, select the full data block including headers, and sort again.
- Excel says it cannot sort merged cells. Unmerge the cells in the range, sort, then reapply merging.
- The header row sorted into the data. Undo, reopen Data > Sort, and tick "My data has headers."
- FORMULATEXT returns #N/A. The referenced cell does not contain a formula. Point it at a formula cell, or wrap it in IFERROR to hide the error.
- Hidden rows did not move. Clear any active filter (Data > Clear) before sorting.
Frequently Asked Questions
Can Excel sort by the formula itself rather than the result?
Not directly. The Sort dialog works on calculated values. Add a helper column with =FORMULATEXT(cell) to turn each formula into text, sort on that column, then delete the helper.
Why did my formula results change after sorting?
Relative references follow the row, so a formula that pointed at row 2 now points at row 7 if its row moved. Use absolute references ($A$2) for cells that must stay fixed.
Does sorting break VLOOKUP formulas?
It can. VLOOKUP with approximate match (TRUE) requires sorted data, so sorting can change results. Use exact match (FALSE) or XLOOKUP to avoid surprises.
Can I sort formulas in Excel for the web?
Yes, Data > Sort works in Excel for the web, but the dialog has fewer options and FORMULATEXT may not be available in every browser session. Desktop Excel is safer for formula-heavy work.
How do I keep the header row at the top?
Tick "My data has headers" in the Sort dialog. Excel then treats row 1 as labels and sorts only the rows beneath it.
Is there a way to sort without moving the surrounding data?
Only by copying the formulas to a separate area, sorting there, and pasting the results back. Sorting a single column in place will misalign it with the rest of the row.