I need to clear any active filters from a sheet before running a certain macro, this line works just fine IF there is an active filter on
If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData
However if no filters are selected it returns the error
Runtime error '1004';
ShowAllData method of Worksheet class failed
I got that code from an answer to this question Excel 2013 VBA Clear All Filters macro
However that question doesn't explain how to ignore the line if no filters are active.
How do I ignore this line if there are no currently active filters applied?
EDIT
For example, all column headings have been auto filtered, so if my sheet is filtered by 'Female' for example I need to remove that filter before running the macro, however if no filters have been applied, just run the macro as normal
To clear filters on a single column we use the AutoFilter method. We only reference the Field parameter and set the value to the number of the column we want to clear. The Field is the column number of the range the filters are applied to, NOT the column number of the worksheet.
If you want to completely remove filters, go to the Data tab and click the Filter button, or use the keyboard shortcut Alt+D+F+F.
Clear all filters in a worksheet and redisplay all rowsOn the Home tab, in the Editing group, click Sort & Filter, and then click Clear.
In the workbook you need to clear filters from, please press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window. 3. Press the F5 key to run the code. Then all filters across all worksheets in current workbook are cleared immediately.
Use FilterMode instead of AutoFilterMode. I have dealt with filters frequently and this code works fine.
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Make sure the worksheet is not protected as this also gives the 1004 error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With