Automating data extraction and report generation in Microsoft Excel using VBA macros often requires clearing active auto-filters before applying new filtering parameters. Developers typically write ActiveSheet.ShowAllData or ws.ShowAllData to reveal all hidden rows. However, if the worksheet is currently not filtered—meaning all rows are already fully visible—calling ShowAllData triggers Run-time error ‘1004’: ShowAllData method of Worksheet class failed. This error occurs because Excel prohibits resetting an auto-filter that contains no active filter criteria.
Guard the ShowAllData Statement with FilterMode Verification
The standard, robust method in professional VBA programming tests the worksheet’s FilterMode boolean property before invoking the clearing method:
Sub ResetFiltersSafely()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Orders")
' Check if any auto-filter is actively hiding data
If ws.FilterMode Then
ws.ShowAllData
End If
' Proceed with subsequent data analysis or custom filters
MsgBox "Filters cleared safely without runtime errors.", vbInformation
End Sub
By wrapping the call in If ws.FilterMode Then, the macro executes smoothly whether filters are active or already empty.
Differentiate Between AutoFilters and ListObject Table Filters
If your tabular data is formatted as an official Excel Table (ListObject), clearing the worksheet filter does not reset table-level column criteria:
Sub ResetTableFilters()
Dim ws As Worksheet
Dim loTable As ListObject
Set ws = ThisWorkbook.Sheets("Orders")
Set loTable = ws.ListObjects("OrdersTable")
' Check table-specific auto-filter mode
If loTable.AutoFilter.FilterMode Then
loTable.AutoFilter.ShowAllData
End If
End Sub
Verify Sheet Protection
If the worksheet is locked with protection, ensure AllowFiltering:=True was enabled during sheet protection, or call ws.Unprotect Password:="pass" before clearing filters.