Synchronizing 2 pivot tables with Page and Data Fields
Here is an example of synchronizing two pivot tables on the same worksheet, with the Page Fields 'DATE', and the Data Fields 'STATE', see below:
Here is the myMaster pivot table (sorry, I am having trouble pasting in myMaster - it looks just like mySlave, but it starts in cell A1):
Here is the mySlave pivot table:
synchronize pivottables.xls |
---|
|
---|
| F | G | H | I |
---|
1 | DATE | (All) | | |
---|
2 | | | | |
---|
3 | SumofUnits | CHANNEL | | |
---|
4 | STATE | Retail | Wholesale | |
---|
5 | WA | 32,902 | 121,115 | |
---|
6 | AZ | 7,441 | 17,900 | |
---|
7 | CA | 21,795 | 90,590 | |
---|
8 | ID | 1,860 | | |
---|
9 | NV | 37,509 | 14,125 | |
---|
10 | OR | 58,721 | 120,795 | |
---|
11 | UT | 3,623 | 36,445 | |
---|
12 | GrandTotal | 163,851 | 400,970 | |
---|
|
---|
Here is the modified code:
'------------------Code Begins Here------------------------------------
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Worksheet_SelectionChange(ByVal Target As Range)
' Macro created 09/22/2006 by Stanley D. Grom, Jr.
'
' Synchronize mySlave PivotTable PageField "DATE" with
' myMaster PivotTable PageField "DATE"
'
Dim myMaster As PivotTable
Dim mySlave As PivotTable
'Dim strPgName As String
Set myMaster = Worksheets("PivotTables").Range("A1").PivotTable
Set mySlave = Worksheets("PivotTables").Range("F1").PivotTable
mySlave.PivotFields("DATE").CurrentPage = myMaster.PivotFields("DATE").CurrentPage.Name
'With ActiveSheet.PivotTables("myMaster").PivotFields("STATE")
' .PivotItems("WA").Visible = True
' .PivotItems("AZ").Visible = True
' .PivotItems("CA").Visible = True
' .PivotItems("ID").Visible = True
' .PivotItems("NV").Visible = True
' .PivotItems("OR").Visible = True
' .PivotItems("UT").Visible = True
'End With
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("WA").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("WA").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("WA").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("WA").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("AZ").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("AZ").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("AZ").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("AZ").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("CA").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("CA").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("CA").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("CA").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("ID").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("ID").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("ID").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("ID").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("NV").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("NV").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("NV").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("NV").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("OR").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("OR").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("OR").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("OR").Visible = False
End If
If ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("UT").Visible = True Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("UT").Visible = True
ElseIf ActiveSheet.PivotTables("myMaster").PivotFields("STATE").PivotItems("UT").Visible = False Then
ActiveSheet.PivotTables("mySlave").PivotFields("STATE").PivotItems("UT").Visible = False
End If
End Sub
'------------------Code Ends Here--------------------------------------
I hope this works for you.
I am using Windows XP Professional SP2, and Excel 2003 SP2.
Have a great day,
Stan