VBA to reapply filters on multiple worksheets

starryeyed223

New Member
Joined
Sep 30, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with 10 worksheets. The "DATA" worksheet is where all data is entered/changes are made. The other 9 worksheets contain cell references to "DATA" with different filters applied. I'm trying to come up with some VBA code that will automatically reapply the filters on all 9 worksheets whenever "DATA" is updated.

So far, I've been able to find code that will automatically reapply the filter to one worksheet - I'm hoping to modify it to include the remaining 8 worksheets, but I don't know enough about VBA to accomplish that. I have the code below entered on the "DATA" worksheet and it's successfully reapplying the filter to the "VIOLATIONS" worksheet.

Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveWorkbook.Worksheets("VIOLATIONS").ListObjects("Table2")
.AutoFilter.ApplyFilter
End With
End Sub

I'm hoping to get some help with modifying this code to reapply the filters on the following worksheets as well (table names in parentheses):
AR (Table3), CD (Table4), CH (Table5), JL (Table6), KK (Table7), KS (Table8), SK (Table9), TB (Table10)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
hi,

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lst As ListObject
Dim wk As Worksheet
  For Each wk In ThisWorkbook.Worksheets
    For Each lst In wk.ListObjects
        lst.Range.AutoFilter
    Next lst
  Next wk
End Sub

This will activate Auto Filter on all of your worksheets once your worksheet changes. As for the application of the filter, each table fields and criteria will be required to automatically apply the filter.

hth...
 
Upvote 0
Maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("Violations", "Table2", "AR", "Table3", "CD", "Table4")
   For i = 0 To UBound(Ary) Step 2
      With ActiveWorkbook.Worksheets(Ary(1)).ListObjects(Ary(i + 1))
         .AutoFilter.ApplyFilter
      End With
   Next i
End Sub
Just add the rest of the sheets & table names to the array
 
Upvote 0
hi,

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lst As ListObject
Dim wk As Worksheet
  For Each wk In ThisWorkbook.Worksheets
    For Each lst In wk.ListObjects
        lst.Range.AutoFilter
    Next lst
  Next wk
End Sub

This will activate Auto Filter on all of your worksheets once your worksheet changes. As for the application of the filter, each table fields and criteria will be required to automatically apply the filter.

hth...
I think I did something wrong because I tried this and it removed the filters from all of the worksheets in my workbook. Could you explain what you mean by "each table fields and criteria will be required to automatically apply the filter"?
 
Upvote 0
I think I did something wrong because I tried this and it removed the filters from all of the worksheets in my workbook.
No, you did nothing wrong. That's exactly what the code does.
 
Upvote 0
Maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Ary As Variant
   Dim i As Long
  
   Ary = Array("Violations", "Table2", "AR", "Table3", "CD", "Table4")
   For i = 0 To UBound(Ary) Step 2
      With ActiveWorkbook.Worksheets(Ary(1)).ListObjects(Ary(i + 1))
         .AutoFilter.ApplyFilter
      End With
   Next i
End Sub
Just add the rest of the sheets & table names to the array
Thank you! I added the rest of the worksheet/table names and tried the code. When I changed some of the data to test whether the filters would update, it gave me an error message saying "Run-time error '9': Subscript out of range". Screenshots of the error and the debug are attached. What did I do wrong?
 

Attachments

  • VBA Runtime Error 9.png
    VBA Runtime Error 9.png
    3.3 KB · Views: 91
  • Debug.png
    Debug.png
    18.5 KB · Views: 91
Upvote 0
Oops, typo it should be
Rich (BB code):
With ActiveWorkbook.Worksheets(Ary(i)).ListObjects(Ary(i + 1))
 
Upvote 0
Solution
I think I did something wrong because I tried this and it removed the filters from all of the worksheets in my workbook. Could you explain what you mean by "each table fields and criteria will be required to automatically apply the filter"?
as @Fluff said, u did it right... and thats exactly what the code does, since i missed a flag, running it again would activate the autofilter. as for criteria and field i meant, which coloumns to filter on which criteria.
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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