by Allen Wyatt
(last updated November 5, 2016)
On many of his worksheets, Roy uses sorting and filtering extensively. On the Data tab of the ribbon, in the Sort & Filter group, there is a Clear tool. This tool clears all filtering and sorting settings. Roy routinely needs to clear the filtering settings, but he would like his sorting settings to remain unaffected by the clearing. He wonders if there is a way to either change how this tool behaves or to clear only the filtering settings in a single action.
This was an interesting problem to research, and it took a bit of poking and prodding. It appears that Excel allows you to define sorting settings for the filters you apply to a data set. To see this in action, follow these steps:
Note that the drop-down menu that appears allows you to select which data is filtered in the column. This is where people normally stop looking, though. It is interesting that at the top of the drop-down menu there are some sorting controls. If you use these controls, then the filtered results that Excel displays are filtered according to your specifications.
If you turn on the macro recorder at this point (after applying a filter that includes sorting) and click the Clear tool, this is the macro that is recorded by Excel:
Sub Macro1() ' ' Macro1 Macro ' ' ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear ActiveSheet.ShowAllData End Sub
Note that there are two lines in the recorded macro. The first line clears the sorting settings and the second clears all the filtering settings. If you record the same steps without having first chosen a sorting setting in the drop-down filtering menu at the top of a column, then Excel doesn't include the first line.
The upshot of this is that you can easily create your own single-line macro that removes any filtering but retains any sorting settings made through the filtering drop-down. The simple macro would look like this:
Sub ClearFilter() ActiveSheet.ShowAllData End Sub
It should be noted that if you turn off filtering (by clicking a second time on the Filter tool), Excel automatically clears any filtering and sorting settings you may have applied. If you want to retain sorting settings—particularly complex sorting settings—outside of the filtering framework, then it would be best to record a macro of the steps you go through for sorting your data.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12254) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
The filtering feature in Excel allows you to quickly copy unique information from one data list to another. If you want to ...Discover More
The advanced filtering capabilities of Excel allow you to easily perform comparisons and calculations while doing the ...Discover More
The filtering capabilities of Excel are indispensable when working with large sets of data. When you create a filtered list, ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.