I am trying to connect a Slicer to multiple Pivot Tables using the method described here: http://dailydoseofexcel.com/archives/2014/08/05/slicers-and-slicercaches/
First, I loop through my Pivot Tables and create a slicer for each:
wkbDash.SlicerCaches.Add(wksPivots.PivotTables(sPTName), sSlicerName). _
Slicers.Add wksSlicers, , sSlicerName, sSlicerName, 1, 1, 50, 100
Then I loop through my Slicers and try to hook them to each Pivot Table
Problem: I am getting an Error #424 "Object Required" on this line:
wkbDash.SlicerCaches(objSlicer).PivotTables.AddPivotTable
(wksPivots.PivotTables(varPTNames(i, 1)))
Code to hook Slicers to Pivot Tables:
' Declarations
Dim objSlicerCache As SlicerCache
Dim objSlicer As Slicer
Dim objPT As PivotTable
Dim varPTNames As Variant
Dim wksDefPivots As Worksheet
Dim wkbDash As Workbook
Dim i As Integer
' Initialize Variables
Set wkbDash = Workbooks(sDash)
Set wksDefPivots = Workbooks(sDash).Worksheets(sDefPivots)
varPTNames = wksDefPivots.Range("A2:A" & FindLastRow(wksDefPivots)).Value2
i = 0
' Procedure
For Each objSlicerCache In Workbooks(sDash).SlicerCaches
For Each objSlicer In objSlicerCache.Slicers
For i = LBound(varPTNames) To UBound(varPTNames)
wkbDash.SlicerCaches(objSlicer).PivotTables.AddPivotTable (wksPivots.PivotTables(varPTNames(i, 1)))
Next i
Next objSlicer
Next objSlicerCache
Based on Bob's comments I have modified the loop to try and hook the Pivot Tables to the SlicerCache object instead of the Slicer. I am still getting the Error #424 "Object Required"
Revised Loop:
For Each objSlicerCache In Workbooks(sDash).SlicerCaches
For j = LBound(varPTNames) To UBound(varPTNames)
objSlicerCache.PivotTables.AddPivotTable (wksPivots.PivotTables(varPTNames(j, 1)))
Next j
Next objSlicerCache
Major Update:
I Have revised the code and am very close. New code sequence:
1. Create 1 PivotCache
2. Use PivotCache to create many PivotTables
3. Create 1 SlicerCache from first PivotTable
4. Add additional PivotTables to SlicerCache
5. Loop through each PivotTable in SlicerCache and create a slicer.
Good news: All this code works. It creates many slicers, and each slicer will filter all the Pivot Tables
The Problem: All the Slicers are only filtering One Field (See Image) rather than each slicer providingng a different field to filter.
Code:
For Each objSlicerCache In wkbDash.SlicerCaches
objSlicerCache.Delete
Next objSlicerCache
sRF = Worksheets(sDefPivots).Range("B2").Value
sPT = Worksheets(sDefPivots).Range("A2").Value
Set objPT = Worksheets(sPivots).PivotTables(sPT)
Set objPF = Worksheets(sPivots).PivotTables(sPT).PivotFields(sRF)
Set objSlicerCache = wkbDash.SlicerCaches.Add(objPT, objPF.Name)
Set objPT = Nothing
Set objPF = Nothing
For Each objPT In Worksheets(sPivots).PivotTables
objSlicerCache.PivotTables.AddPivotTable objPT
Next objPT
For Each objSlicerCache In wkbDash.SlicerCaches
For Each objPT In objSlicerCache.PivotTables
objSlicerCache.Slicers.Add wksSlicers, , objPT.Name, objPT.Name, 1, 1, 50, 100
Next objPT
Next objSlicerCache