Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim ptDetail As PivotTable, ptSummary As PivotTable
Dim pi As PivotItem, pf As PivotField
Dim avarSelected()
Dim lngIndex As Long
Dim rngData As Range
If Target.Name = "PivotTable7" Then
On Error Resume Next
Application.EnableEvents = False
Set ptSummary = Target
Set ptDetail = PivotTable1
Set pf = ptSummary.PivotFields("Branch")
' get list of visible IDs in summary table
For Each pi In pf.VisibleItems
Set rngData = pi.DataRange
If Not rngData Is Nothing Then
ReDim Preserve avarSelected(lngIndex)
avarSelected(lngIndex) = pi.Name
lngIndex = lngIndex + 1
Set rngData = Nothing
End If
Next pi
' update detail table
With ptDetail
.ManualUpdate = True
With .PivotFields("Branch")
' need at least one visible!
.PivotItems("" & avarSelected(0)).Visible = True
For Each pi In .PivotItems
pi.Visible = Not IsError(Application.Match(pi.Name, avarSelected, 0))
Next pi
End With
.ManualUpdate = False
End With
Application.EnableEvents = True
End If
End Sub
|