Jerry Sullivan
MrExcel MVP
- Joined
- Mar 18, 2010
- Messages
- 8,787
When the user filters Table1, my objective is to have Table2 on the same sheet filtered so that both tables have the same ID items visible.
Since VBA doesn't have an event that is triggered when a table is filtered, I'm using the Worksheet_Calculate event which is triggered due to the presence a SUBTOTAL formula that counts visible rows in Table1.
The problem is that VBA doesn't seem to be able to AutoFilter Table2 if Worksheet_Calculate is triggered as a result of filtering Table1.
Here's a simplified example to illustrate the problem...
Note: My finished code will read the visible items' IDs in Table1 into an array. I've removed that step for this example to demonstrate that isn't causing the anomaly.
If I enter any value in cell B21, Worksheet_Calculate is triggered and the code successfully filters Table2 to show ID's 111,222, and 444.
If I filter Table1, Worksheet_Calculate is triggered and the code unexpectedly applies the filters intended for Table2 to the ID field of Table1!
If I Cut and Paste Table2 into Sheet2 (and modify the code to reference it on Sheet2), when Table1 is filtered, the code successfully filters Table2 to show ID's 111,222, and 444.
The code will also work if I eliminate the Worksheet_Calculate and require the user to push a button to call SyncFiltersByID.
The best workaround I've found is to add a Slicer to the ID field of Table2, and sync the tables through the Slicer. I'll go with that unless someone can suggest a better solution. Note that the Tables do not use the same data source, so they can't be sync'd by slicers alone.
TIA
Since VBA doesn't have an event that is triggered when a table is filtered, I'm using the Worksheet_Calculate event which is triggered due to the presence a SUBTOTAL formula that counts visible rows in Table1.
The problem is that VBA doesn't seem to be able to AutoFilter Table2 if Worksheet_Calculate is triggered as a result of filtering Table1.
Here's a simplified example to illustrate the problem...
Excel 2016 (Windows) 32 bit | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | Table1 | |||||||
2 | ID | Name | Measure | |||||
3 | #111 | Name1 | 277 | Test | 4 | |||
4 | #222 | Name2 | 155 | Formula: | 5 | |||
5 | #333 | Name3 | 296 | |||||
6 | #444 | Name4 | 109 | |||||
7 | Subtotal: | 837 | ||||||
8 | ||||||||
9 | Table 2 | |||||||
10 | ID | Name | Measure | |||||
11 | #111 | Name1 | 637 | |||||
12 | #222 | Name2 | 476 | |||||
13 | #333 | Name3 | 454 | |||||
14 | #444 | Name4 | 127 | |||||
15 | Subtotal: | 1694 | ||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
F4 | =F3+1 | |
C7 | =SUBTOTAL(9,Table1[Measure]) | |
C15 | =SUBTOTAL(9,Table2[Measure]) |
Note: My finished code will read the visible items' IDs in Table1 into an array. I've removed that step for this example to demonstrate that isn't causing the anomaly.
Code:
'--worksheet code in Sheet1
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
SyncFiltersByID
Application.EnableEvents = True
End Sub
Code:
'--code in Module1
Sub SyncFiltersByID()
Dim vVisibleItemList As Variant
'--simplified for this example- will read Table1 in final code
vVisibleItemList = Array("#111", "#333", "#444")
'--sync Table2 to list of IDs in Table1
With Sheet1.ListObjects("Table2").Range
.AutoFilter Field:=1, _
Criteria1:=Array(vVisibleItemList), Operator:=xlFilterValues
End With
End Sub
If I enter any value in cell B21, Worksheet_Calculate is triggered and the code successfully filters Table2 to show ID's 111,222, and 444.
If I filter Table1, Worksheet_Calculate is triggered and the code unexpectedly applies the filters intended for Table2 to the ID field of Table1!
If I Cut and Paste Table2 into Sheet2 (and modify the code to reference it on Sheet2), when Table1 is filtered, the code successfully filters Table2 to show ID's 111,222, and 444.
The code will also work if I eliminate the Worksheet_Calculate and require the user to push a button to call SyncFiltersByID.
The best workaround I've found is to add a Slicer to the ID field of Table2, and sync the tables through the Slicer. I'll go with that unless someone can suggest a better solution. Note that the Tables do not use the same data source, so they can't be sync'd by slicers alone.
TIA
Last edited: