Clearing a sort in Excel VBA removes any existing sort settings from a range or table, restoring the data to its original order or allowing you to apply a new sort without conflicts. This is essential when automating reports or cleaning up data before re-sorting, as leftover sort fields can cause errors or unexpected results. No external tools or add-ins are required—just Excel with VBA enabled.

What You'll Need

  • Microsoft Excel 2010 or later (VBA supported)
  • A worksheet with sorted data or a defined range
  • Developer tab enabled in Excel (to access the VBA editor)
  • Basic understanding of Excel ranges and worksheet objects

Step-by-Step

  1. Open your Excel workbook containing the data you want to clear the sort from.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
  3. In the Project Explorer window on the left, locate your workbook (e.g., VBAProject (YourWorkbookName.xlsx)).
  4. Right-click on the workbook name, select Insert, then choose Module to create a new blank code module.
  5. In the new module window, type or paste the following code: Sub ClearSort() on the first line.
  6. On the next line, type: ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear. Replace "Sheet1" with the actual name of your worksheet, including quotes.
  7. On the following line, type: ActiveWorkbook.Worksheets("Sheet1").Sort.SetRange Range("A1:D100"). Adjust the range to match the exact data area you want to clear the sort from (e.g., "A1:Z500").
  8. On the next line, type: ActiveWorkbook.Worksheets("Sheet1").Sort.Header = xlYes if your data has headers; use xlNo if it doesn’t.
  9. On the next line, type: ActiveWorkbook.Worksheets("Sheet1").Sort.Apply to execute the clear operation.
  10. On the next line, type: End Sub to close the subroutine.
  11. Press F5 while your cursor is inside the subroutine to run the code immediately.
  12. Switch back to Excel and verify the sort has been cleared by checking that the filter arrows in the header row no longer display a sort indicator (small arrow icon next to column headers).
  13. To reuse this code later, save your workbook as a macro-enabled file (.xlsm).

Common Mistakes

  • Using the wrong worksheet name in the code—Excel throws a runtime error if the sheet doesn’t exist exactly as typed.
  • Forgetting to define the range; if the range is too small or too large, some sort fields may remain or cause errors.
  • Not setting the Header property correctly—if your data has headers but you set it to xlNo, Excel may treat the first row as data and corrupt your sort.
  • Running the code on a filtered range without clearing filters first; filters and sorts interact unpredictably.
  • Using Selection instead of a defined range—this makes the code unreliable if the user selects a different range before running it.
  • Typing Sort.Clear instead of Sort.SortFields.Clear—the former doesn’t exist and causes a compile error.

Tips

  • Always test your VBA code on a copy of your data first—never on live reports until you’re certain it works.
  • Use named ranges (e.g., Range("DataRange")) instead of hard-coded cell references for better maintainability.
  • Add error handling with On Error Resume Next before the sort clear line and On Error GoTo 0 after if you’re working with unreliable data sources.
  • Combine this with Range.AutoFilter Mode = xlOff to clear both filters and sorts in one pass.
  • Use Debug.Print ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Count before and after to verify how many sort fields were removed.
  • When working with Excel Tables (ListObjects), use ListObjects("TableName").Sort.SortFields.Clear instead of worksheet-based syntax.

Troubleshooting

  • Runtime Error 9: Subscript out of range—This means the worksheet name in your code doesn’t match the actual sheet name. Check for extra spaces, capitalization, or special characters. Use Debug.Print ActiveWorkbook.Worksheets.Count to list all sheet names in the Immediate Window (Ctrl+G in VBA editor).
  • Sort doesn’t clear—filter arrows still show sort icons—You may have multiple sort fields. Loop through them: use a For Each loop on SortFields to clear each individually, or ensure you’re calling .Clear on the SortFields collection, not the Sort object.
  • Code runs but nothing changes—Verify the range you specified includes the header row and actual sorted columns. If the range is only one row, the sort has no effect.
  • Compile error: Method or data member not found—You likely typed Sort.Clear instead of Sort.SortFields.Clear. Double-check the syntax.
  • Excel freezes or becomes unresponsive—Stop the macro (Ctrl+Break), then disable screen updating at the start of your code with Application.ScreenUpdating = False and re-enable it at the end with Application.ScreenUpdating = True.
  • Sort is cleared but data is reordered unexpectedly—This usually means there’s an active AutoFilter. Clear it with Range.AutoFilter before clearing the sort.

Frequently Asked Questions

Can I clear sort on multiple sheets at once?

Yes. Wrap the clear sort code in a loop that iterates through each worksheet. Use For Each ws In ActiveWorkbook.Worksheets and apply the same logic to each ws.Sort.SortFields.Clear.

Does clearing sort delete my data?

No. Clearing sort only removes the sort criteria—your data remains intact. The order may revert to original input order, but no values are deleted or changed.

Why does my sort keep coming back after I clear it?

Another macro, pivot table refresh, or external data connection may be reapplying the sort. Check for other VBA routines triggered by events like Worksheet_Change or Workbook_SheetChange.

Can I clear sort without using VBA?

Yes, manually: select your data, go to Data > Sort > Clear, then click OK. But VBA is needed for automation, especially in reports that run daily or with multiple users.

What’s the difference between clearing sort and removing filters?

Clearing sort removes the column order criteria (e.g., sort by Name ascending). Removing filters hides the dropdown arrows and stops filtering data. You can have one without the other—use .AutoFilterMode = False to remove filters.

Do I need to save as .xlsm every time I clear sort?

You only need to save as .xlsm if you want to keep the VBA code for future use. If you run the code once and close the file without saving, the macro will be lost. Always save macro-enabled workbooks as .xlsm.