'Variable to prevent event looping
Dim mbNoEvent As Boolean
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
'
'
Dim oScMedewerker As SlicerCache
Dim oScMedewerker1 As SlicerCache
Dim oSc As SlicerCache
Dim oPT As PivotTable
Dim oSi As SlicerItem
Dim bUpdate As Boolean
'Prevent event looping, changing a slicer in this routine also triggers this routine
If mbNoEvent Then Exit Sub
mbNoEvent = True
'Let it run in the background to speed things up
bUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
'step through all SlicerCaches and then through all their associated pivottables to find out which slicers are tied to the changed pivottable:
For Each oSc In ThisWorkbook.SlicerCaches
For Each oPT In oSc.PivotTables
If oPT.Name = Target.Name And oPT.Parent.Name = Target.Parent.Name Then
If oSc.Name Like "*Slicer_Medewerker*" Then
Set oScMedewerker = oSc
ElseIf oSc.Name Like "*Slicer_Medewerker1*" Then
Set oScMedewerker1 = oSc
End If
Exit For
End If
Next
If Not oScMedewerker Is Nothing And Not oScMedewerker1 Is Nothing Then Exit For
Next
'Now that we have the slicer(s) that have been clicked, we want to sync them with slicers with similar names
'what the code below does is loop through all slicercaches again, looking at their names to figure out which ones belong together.
If Not oScMedewerker Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScMedewerker.Name, 7, 3) And oSc.Name <> oScMedewerker.Name Then
'This one has a similar fieldname (first three characters are compared in this case)
'but not the same name, as that would be the same slicercache.
'So synch it with the changed year slicer
'If a slicer has the very first item selected and you subsequently de-select it,
'the end result is that all sliceritems get selected. So select the last item of the slicer first
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScMedewerker.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
If Not oScMedewerker1 Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScMedewerker1.Name, 7, 3) And oSc.Name <> oScMedewerker1.Name Then
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScMedewerker1.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
mbNoEvent = False
Application.ScreenUpdating = bUpdate
End Sub
Private Sub Workbook_SheetPivotTableUpdate2(ByVal Sh As Object, ByVal Target As PivotTable)
'
'
Dim oScDatum As SlicerCache
Dim oScDatum1 As SlicerCache
Dim oScDatum2 As SlicerCache
Dim oScDatum3 As SlicerCache
Dim oSc As SlicerCache
Dim oPT As PivotTable
Dim oSi As SlicerItem
Dim bUpdate As Boolean
'Prevent event looping, changing a slicer in this routine also triggers this routine
If mbNoEvent Then Exit Sub
mbNoEvent = True
'Let it run in the background to speed things up
bUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
'step through all SlicerCaches and then through all their associated pivottables to find out which slicers are tied to the changed pivottable:
For Each oSc In ThisWorkbook.SlicerCaches
For Each oPT In oSc.PivotTables
If oPT.Name = Target.Name And oPT.Parent.Name = Target.Parent.Name Then
If oSc.Name Like "*Slicer_Datum*" Then
Set oScDatum = oSc
ElseIf oSc.Name Like "*Slicer_Datum1*" Then
Set oScDatum1 = oSc
ElseIf oSc.Name Like "*Slicer_Datum2*" Then
Set oScDatum2 = oSc
ElseIf oSc.Name Like "*Slicer_Datum3*" Then
Set oScDatum3 = oSc
End If
Exit For
End If
Next
If Not oScDatum Is Nothing And Not oScDatum1 Is Nothing And Not oScDatum2 Is Nothing And Not oScDatum3 Is Nothing Then Exit For
Next
'Now that we have the slicer(s) that have been clicked, we want to sync them with slicers with similar names
'what the code below does is loop through all slicercaches again, looking at their names to figure out which ones belong together.
If Not oScDatum Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScDatum.Name, 7, 3) And oSc.Name <> oScDatum.Name Then
'This one has a similar fieldname (first three characters are compared in this case)
'but not the same name, as that would be the same slicercache.
'So synch it with the changed year slicer
'If a slicer has the very first item selected and you subsequently de-select it,
'the end result is that all sliceritems get selected. So select the last item of the slicer first
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScDatum.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
If Not oScDatum1 Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScDatum1.Name, 7, 3) And oSc.Name <> oScDatum1.Name Then
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScDatum1.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
If Not oScDatum2 Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScDatum2.Name, 7, 3) And oSc.Name <> oScDatum2.Name Then
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScDatum2.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
If Not oScDatum3 Is Nothing Then
For Each oSc In ThisWorkbook.SlicerCaches
If Mid(oSc.Name, 7, 3) = Mid(oScDatum3.Name, 7, 3) And oSc.Name <> oScDatum3.Name Then
oSc.SlicerItems(oSc.SlicerItems.Count).Selected = True
For Each oSi In oScDatum3.SlicerItems
On Error Resume Next
If oSc.SlicerItems(oSi.Value).Selected <> oSi.Selected Then
oSc.SlicerItems(oSi.Value).Selected = oSi.Selected
End If
Next
End If
Next
End If
mbNoEvent = False
Application.ScreenUpdating = bUpdate
End Sub