Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel 2013 VBA clear active filter

Tags:

excel

vba

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

like image 582
Tim Wilkinson Avatar asked Jan 22 '15 10:01

Tim Wilkinson


People also ask

How do I clear filters in Excel VBA?

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.

How do I clear active filters in Excel?

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.

How do you remove filters in Excel 2013?

Clear all filters in a worksheet and redisplay all rowsOn the Home tab, in the Editing group, click Sort & Filter, and then click Clear.

How do I remove a filter from all sheets in Excel VBA?

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.


1 Answers

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.

like image 162
Paul Kelly Avatar answered Oct 06 '22 17:10

Paul Kelly