Clearing all filters in Excel using VBA removes any active AutoFilter or Advanced Filter settings across all worksheets in a workbook, restoring raw data views instantly. This requires only Excel with VBA enabled—no extra tools or add-ins needed.
What You'll Need
- Microsoft Excel (2010 or later, including Office 365 and Excel 2021)
- A workbook containing filtered data (optional; the code works even with no filters applied)
- VBA enabled: ensure macros are permitted in Excel Trust Center settings
Step-by-Step
- Open your Excel workbook containing filtered data.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to create a new blank code module.
- Copy and paste the following VBA code into the module window:
Sub ClearAllFilters() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets If ws.AutoFilterMode Then ws.ShowAllData End If Next ws MsgBox "All filters cleared across all worksheets.", vbInformation End Sub - Close the VBA editor by clicking the X or pressing Alt + Q.
- Press Alt + F8 to open the Macro dialog box.
- Select ClearAllFilters from the list and click Run.
Common Mistakes
- Running code on a protected sheet: If a worksheet is protected,
ws.ShowAllDatawill fail. Unprotect sheets first (Review > Unprotect Sheet) or modify the code to unprotect programmatically (with password if needed). - Using ActiveSheet instead of looping: Using
ActiveSheet.ShowAllDataonly clears filters on the currently active sheet. The provided loop ensures all sheets are processed. - Forgetting to save as macro-enabled: If you save the workbook as .xlsx instead of .xlsm, the macro will be lost. Always choose Excel Macro-Enabled Workbook (*.xlsm) when saving.
- Assuming filters exist: The code checks
AutoFilterModeto avoid errors, but if filters were applied viaRange.AutoFilterwithout headers,ShowAllDatamay throw a runtime error. This script avoids that by checking mode first.
Tips
- Assign to a button: For frequent use, insert a form control button (Developer tab > Insert > Button), assign ClearAllFilters to it, and place it near your data.
- Include Advanced Filters: This script only clears AutoFilter. To clear Advanced Filter criteria ranges, add code like
ws.Range("CriteriaRange").Clear(replace with your actual range). - Undo safely: VBA actions cannot be undone with Ctrl + Z. Add
Application.Undoonly if you stored the previous state in a variable—a complex task. Simpler: run the macro on a copied backup first. - Suppress alerts: Add
Application.ScreenUpdating = Falseat the start andTrueat the end for large workbooks to speed up execution and prevent flickering.
Troubleshooting
If the macro fails or doesn’t clear filters, follow these checks:
- Check macro security: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros (temporarily for testing) or Disable all macros with notification and click Enable Macros when prompted.
- Verify sheet protection: If a sheet is protected, unprotect it manually (Review tab > Unprotect Sheet) or update the code to unprotect first:
Sub ClearAllFilters() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets ws.Unprotect Password:="yourpassword" 'replace or use ws.Unprotect if no password If ws.AutoFilterMode Then ws.ShowAllData End If Next ws End SubNote: Hard-coding passwords in code is insecure; store passwords securely or remove protection before running. - Check for hidden filters: Sometimes filters remain visually hidden. Manually go to each sheet, click any cell in the data range, and press Ctrl + Shift + L to toggle filters off.
- Test with a single sheet: Replace
ThisWorkbook.WorksheetswithWorksheets("Sheet1")temporarily to isolate the issue on one sheet. - Runtime error 1004: This often means
ShowAllDataran when filters weren’t active. The provided code avoids this with theIf ws.AutoFilterModecheck—if you modified the code and see this error, restore that condition.
Frequently Asked Questions
Does this clear filters from PivotTables?
No. This script only clears AutoFilter on regular data ranges. To clear PivotTable filters, use ws.PivotTables("PivotName").ClearAllFilters or loop through all pivot tables on each sheet.
Can I clear filters on only one specific sheet?
Yes. Change the loop to target one sheet:
Sub ClearSheetFilters()
With ThisWorkbook.Worksheets("Data")
If .AutoFilterMode Then .ShowAllData
End With
End SubReplace "Data" with your sheet name.
Will clearing filters delete my data?
No. VBA’s ShowAllData only removes filter criteria—it does not delete rows, columns, or values. Your original dataset remains intact.
Why does the message box not appear after running?
Excel macros can be set to suppress message boxes. Check if Application.DisplayAlerts = False is used elsewhere (e.g., in Workbook_Open). Add Application.DisplayAlerts = True before the MsgBox line to restore alerts.
Does this work in Excel Online or mobile?
No. VBA macros are not supported in Excel for the web or mobile apps. You must open the workbook in desktop Excel (Windows or macOS) to run this code.
How do I make the macro run automatically when I open the file?
Use the Workbook_Open event. In the ThisWorkbook code module (double-click ThisWorkbook in the Project Explorer), paste:
Private Sub Workbook_Open()
Call ClearAllFilters
End SubBe cautious—this runs every time the file opens, potentially hiding important filtered views.