How to use a loop to clear all filters in all tables in a workbook?

M McAllister

New Member
Joined
Apr 16, 2013
Messages
15
Windows 7, Excel 2010

I have workbook with 6 data tables fed by data connections. When I close the workbook, I want to clear all the filters in each of the tables before saving and closing the workbook.

I have a lazy macro that loops through the each table and sheet, but doesn't clear the table filters.

Code:
Dim ws As Worksheet
Dim lo As ListObject
    For Each ws In ActiveWorkbook.Worksheets
    For Each lo In ws.ListObjects
        ws.Activate
        With ActiveSheet
            If .AutoFilterMode Then
                If .FilterMode Then
                    .ShowAllData
                End If
            End If
        End With
        
    Next
    Next

Any help is appreciated.

Thanks,
Mathew
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I solved the table update by changing the code to this:

Code:
Dim ws As Worksheet
    Dim lo As ListObject
    For Each ws In ActiveWorkbook.Worksheets
        For Each lo In ws.ListObjects
            If lo.AutoFilter.FilterMode Then
                lo.AutoFilter.ShowAllData
            End If
        Next
    Next

The tables update but it's throwing an error "Run-time error '91': Object variable or With block variable not set". The highligted line in the debug is:

Code:
If lo.AutoFilter.FilterMode Then

I've looked at examples but I don't know how to solve this.
Any help is appreciated. Thank you in advance.

Mathew
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top