Public Function Synch_All_PT_Filters_BasedOn(PT As PivotTable, _
sField1 As String, sField2 As String)
Dim PT2 As PivotTable
Dim vItems1 As Variant
Dim vItems2 As Variant
'---Stores the visible items in an array
vItems1 = Store_PT_FilterItems(PT, sField1)
vItems2 = Store_PT_FilterItems2(PT, sField2)
'---make array of visible items in PT
For Each PT2 In Sheets("general pivot").PivotTables
If PT2.Name <> PT.Name Then
'---Applies same filter items to each PivotTable
Call Filter_PivotField( _
pvtField:=PT2.PivotFields(sField1), _
vItems1:=vItems1)
Call Filter_PivotField2( _
pvtField2:=PT2.PivotFields(sField2), _
vItems2:=vItems2)
End If
Next PT2
End Function
Private Function Store_PT_FilterItems(PT As PivotTable, _
sField1 As String) As Variant
'---Stores visible items in PivotField sField in an array
Dim sVisibleItems() As String
Dim pviItem As PivotItem
Dim i As Long
With PT.PivotFields(sField1)
If .Orientation = xlPageField And _
.EnableMultiplePageItems = False Then
ReDim sVisibleItems(1)
sVisibleItems(0) = .CurrentPage
Else
For Each pviItem In .PivotItems
If pviItem.Visible Then
i = i + 1
ReDim Preserve sVisibleItems(i)
sVisibleItems(i - 1) = pviItem
End If
Next
End If
End With
Store_PT_FilterItems = sVisibleItems
End Function
Private Function Store_PT_FilterItems2(PT As PivotTable, _
sField2 As String) As Variant
'---Stores visible items in PivotField sField in an array
Dim sVisibleItems() As String
Dim pviItem As PivotItem
Dim i As Long
With PT.PivotFields(sField2)
If .Orientation = xlPageField And _
.EnableMultiplePageItems = False Then
ReDim sVisibleItems(1)
sVisibleItems(0) = .CurrentPage
Else
For Each pviItem In .PivotItems
If pviItem.Visible Then
i = i + 1
ReDim Preserve sVisibleItems(i)
sVisibleItems(i - 1) = pviItem
End If
Next
End If
End With
Store_PT_FilterItems2 = sVisibleItems
End Function
Private Function Filter_PivotField(pvtField As PivotField, _
vItems1 As Variant)
'---Filters the PivotField to make stored vItems1 Visible
Dim sItem As String, bTemp As Boolean, i As Long
On Error Resume Next
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
If Not (IsArray(vItems1)) Then
vItems1 = Array(vItems)
End If
With pvtField
.Parent.ManualUpdate = True
If .Orientation = xlPageField Then .EnableMultiplePageItems = True
If vItems1(0) = "(All)" Then
For i = 1 To .PivotItems.Count
If Not .PivotItems(i).Visible Then _
.PivotItems(i).Visible = True
Next i
Else
For i = LBound(vItems1) To UBound(vItems1)
bTemp = Not (IsError(.PivotItems(vItems1(i)).Visible))
If bTemp Then
sItem = .PivotItems(vItems1(i))
Exit For
End If
Next i
If sItem = "" Then
MsgBox "None of filter list items found."
GoTo CleanUp
End If
.PivotItems(sItem).Visible = True
For i = 1 To .PivotItems.Count
If IsError(Application.Match(.PivotItems(i), _
vItems1, 0)) = .PivotItems(i).Visible Then
.PivotItems(i).Visible = Not (.PivotItems(i).Visible)
End If
Next i
End If
End With
CleanUp:
pvtField.Parent.ManualUpdate = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Function
Private Function Filter_PivotField2(pvtField2 As PivotField, _
vItems2 As Variant)
'---Filters the PivotField to make stored vItems Visible
Dim sItem As String, bTemp As Boolean, i As Long
On Error Resume Next
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
If Not (IsArray(vItems2)) Then
vItems2 = Array(vItems2)
End If
With pvtField2
.Parent.ManualUpdate = True
If .Orientation = xlPageField Then .EnableMultiplePageItems = True
If vItems2(0) = "(All)" Then
For i = 1 To .PivotItems.Count
If Not .PivotItems(i).Visible Then _
.PivotItems(i).Visible = True
Next i
Else
For i = LBound(vItems2) To UBound(vItems2)
bTemp = Not (IsError(.PivotItems(vItems2(i)).Visible))
If bTemp Then
sItem = .PivotItems(vItems2(i))
Exit For
End If
Next i
If sItem = "" Then
MsgBox "None of filter list items found."
GoTo CleanUp
End If
.PivotItems(sItem).Visible = True
For i = 1 To .PivotItems.Count
If IsError(Application.Match(.PivotItems(i), _
vItems2, 0)) = .PivotItems(i).Visible Then
.PivotItems(i).Visible = Not (.PivotItems(i).Visible)
End If
Next i
End If
End With
CleanUp:
pvtField2.Parent.ManualUpdate = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Function