VBA - Set multiple slicers at once in Excel

MrAutomation

New Member
Joined
Jun 18, 2017
Messages
16
Duplicated at Stack Overflow, but no response.
I have a workbook with two pivot table that have the exact same slicers. I'm using a VBA script to synchronize the two slicers. I've named them the same with one as "Slicer_xxxx_Master" and the other as "Slicer_xxxx_Slave".

The code below works fine, except on slicers with lots of options. Since it sets the slices one by one, it re-filters the pivot table over and over again until all the required slices are set.

Is there a method to collect all the slice item values into an array and then set the Slave slicer values all at once?

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Application.ScreenUpdating = False

Dim sC As SlicerCache
Dim sC_Slave As SlicerCache
Dim SL As SlicerCacheLevel
Dim sI As SlicerItem
Dim sI_Slave As SlicerItem


For Each sC In ActiveWorkbook.SlicerCaches
    If InStr(1, sC.Name, "Master") Then
        Set sC_Slave = ThisWorkbook.SlicerCaches(Replace(sC.Name, "Master", "Slave"))
        For Each sI In sC.SlicerItems
            If sI.Name <> ("(blank)") Then
                Set sI_Slave = sC_Slave.SlicerItems(sI.Name)
                If sI_Slave.Selected <> sI.Selected Then
                    sI_Slave.Selected = sI.Selected
                End If
            End If
        Next
    End If
Next

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top