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

  1. Open your Excel workbook containing filtered data.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
  3. In the VBA editor, go to Insert > Module to create a new blank code module.
  4. 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
  5. Close the VBA editor by clicking the X or pressing Alt + Q.
  6. Press Alt + F8 to open the Macro dialog box.
  7. Select ClearAllFilters from the list and click Run.

Common Mistakes

  • Running code on a protected sheet: If a worksheet is protected, ws.ShowAllData will 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.ShowAllData only 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 AutoFilterMode to avoid errors, but if filters were applied via Range.AutoFilter without headers, ShowAllData may 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.Undo only 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 = False at the start and True at 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:

  1. 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.
  2. 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 Sub
    Note: Hard-coding passwords in code is insecure; store passwords securely or remove protection before running.
  3. 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.
  4. Test with a single sheet: Replace ThisWorkbook.Worksheets with Worksheets("Sheet1") temporarily to isolate the issue on one sheet.
  5. Runtime error 1004: This often means ShowAllData ran when filters weren’t active. The provided code avoids this with the If ws.AutoFilterMode check—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 Sub
Replace "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 Sub
Be cautious—this runs every time the file opens, potentially hiding important filtered views.