Home > Enterprise >  Clear AutoFilter from Entire Worksheet (Multiple Tables)
Clear AutoFilter from Entire Worksheet (Multiple Tables)

Time:10-05

I am working on a project and I have added a button to autohide/unhide unused rows in my Input Section. I used this on another page that only contained one table and it worked fine. However, on this sheet the button works to apply the filter to all of the tables, but, when I hit the button to unhide it only clears the autofilter from the first table on the sheet but the filters on the other tables remain on. I am not exactly sure what is going on here but I have attached my code below:

Private Sub OpAssumptionsToggle_Click()
 
    'Declarations
    Set ActIncAss = ListObjects("ActualIncomeAssumptions")
    Set ActExpAss = ListObjects("ActualExpenseAssumptions")
    Set S1IncAss = ListObjects("S1IncomeAssumptions")
    Set S1ExpAss = ListObjects("S1ExpenseAssumptions")
    Set S2IncAss = ListObjects("S2IncomeAssumptions")
    Set S2ExpAss = ListObjects("S2ExpenseAssumptions")
    Set PFIncAss = ListObjects("PFIncomeAssumptions")
    Set PFExpAss = ListObjects("PFExpenseAssumptions")
        
    'Select Range
    ActIncAss.Range.Select
    
    'Hide/Unhide Rows
    If ActiveSheet.FilterMode Then
        ActiveSheet.AutoFilter.ShowAllData
    Else
        ActIncAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        ActExpAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        S1IncAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        S1ExpAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        S2IncAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        S2ExpAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        PFIncAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
        PFExpAss.Range.AutoFilter Field:=6, Criteria1:="<>0"
    End If
    
    'Unselect and Select A1
    Application.CutCopyMode = False
    Range("A1").Select

End Sub

CodePudding user response:

Clear the filter for each table; probably easiest to loop:

Dim tbl As ListObject
For Each tbl in ActiveSheet.ListObjects
    tbl.AutoFilter.ShowAllData
Next

Note that you could extend this logic to applying the filter:

For Each tbl in ActiveSheet.ListObjects
    tbl.Range.AutoFilter Field:=6, Criteria1:="<>0"
Next
  • Related