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
- Open your Excel workbook containing the data you want to clear the sort from.
- Press
Alt + F11to open the Visual Basic for Applications (VBA) editor. - In the Project Explorer window on the left, locate your workbook (e.g., VBAProject (YourWorkbookName.xlsx)).
- Right-click on the workbook name, select Insert, then choose Module to create a new blank code module.
- In the new module window, type or paste the following code:
Sub ClearSort()on the first line. - On the next line, type:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear. Replace "Sheet1" with the actual name of your worksheet, including quotes. - 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"). - On the next line, type:
ActiveWorkbook.Worksheets("Sheet1").Sort.Header = xlYesif your data has headers; usexlNoif it doesn’t. - On the next line, type:
ActiveWorkbook.Worksheets("Sheet1").Sort.Applyto execute the clear operation. - On the next line, type:
End Subto close the subroutine. - Press
F5while your cursor is inside the subroutine to run the code immediately. - 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).
- 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
Headerproperty correctly—if your data has headers but you set it toxlNo, 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
Selectioninstead of a defined range—this makes the code unreliable if the user selects a different range before running it. - Typing
Sort.Clearinstead ofSort.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 Nextbefore the sort clear line andOn Error GoTo 0after if you’re working with unreliable data sources. - Combine this with
Range.AutoFilter Mode = xlOffto clear both filters and sorts in one pass. - Use
Debug.Print ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Countbefore and after to verify how many sort fields were removed. - When working with Excel Tables (ListObjects), use
ListObjects("TableName").Sort.SortFields.Clearinstead 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.Countto 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 Eachloop onSortFieldsto clear each individually, or ensure you’re calling.Clearon 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.Clearinstead ofSort.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 = Falseand re-enable it at the end withApplication.ScreenUpdating = True. - Sort is cleared but data is reordered unexpectedly—This usually means there’s an active AutoFilter. Clear it with
Range.AutoFilterbefore 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. UseFor 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 likeWorksheet_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.