Sub topic 15
Automating Slicers with VBA
This chapter assumes you can build a PivotTable, insert a slicer, and connect a slicer to several PivotTables through Report Connections. It also assumes you can open the Visual Basic Editor with Alt+F11 and run a macro. Everything here is optional for daily work. It pays off when you build the same report layout again and again.
What the slicer objects are
Excel splits a slicer into two objects.
- A SlicerCache holds the field, the selected items, and the list of connected PivotTables. It belongs to the workbook.
- A Slicer is the visible box on a sheet. It belongs to a slicer cache.
Each manual step has a code equivalent. Inserting a slicer creates a cache and a slicer. Report Connections edits the cache's PivotTables list. The Clear Filter button calls a method on the cache.
Adding a slicer
This macro creates a slicer cache for the Region field of the first PivotTable on the active sheet. It then places a slicer named RegionSlicer on that sheet.
Sub AddRegionSlicer()
Dim pt As PivotTable
Dim sc As SlicerCache
Set pt = ActiveSheet.PivotTables(1)
Set sc = ActiveWorkbook.SlicerCaches.Add2(pt, "Region")
sc.Slicers.Add ActiveSheet, , "RegionSlicer", "Region", 10, 400, 140, 150
End SubSlicerCaches.Add2 takes the PivotTable and the source field name. Slicers.Add takes the sheet, an optional slicer name (skipped here), the caption, and then the Top, Left, Width, and Height in points. Excel names the cache Slicer_Region by default.
Connecting a second PivotTable
This is the code version of ticking another PivotTable in Report Connections.
Sub ConnectSlicerToSecondPivot()
Dim sc As SlicerCache
Set sc = ActiveWorkbook.SlicerCaches("Slicer_Region")
sc.PivotTables.AddPivotTable ActiveSheet.PivotTables(2)
End SubTwo conditions apply. The second PivotTable must share the same pivot cache as the first, or the call fails. The name Slicer_Region must match the cache name in your workbook.
Clearing the filter
This macro does what the Clear Filter button does. It removes the selection so every item shows again.
Sub ClearRegionSlicer()
ActiveWorkbook.SlicerCaches("Slicer_Region").ClearManualFilter
End SubCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Subscript out of range on `SlicerCaches("...")` | The cache name does not match | Print the names and use the exact one |
| Error when adding a PivotTable to the cache | The PivotTable uses a different pivot cache | Rebuild it from the same source, or copy the first PivotTable |
| Error on `PivotTables(2)` | The sheet has only one PivotTable | Check `ActiveSheet.PivotTables.Count` first |
| Duplicate slicer name error | A slicer named RegionSlicer already exists | Delete it first or choose another name |
Exercise
A macro that calls sc.PivotTables.AddPivotTable fails for a PivotTable on another sheet. The slicer was created from the first PivotTable. What is the most likely reason, and how do you check it?
Show answer
The second PivotTable probably uses a different pivot cache, because it was built from the source data separately instead of being copied from the first. A slicer cache can only connect PivotTables that share one pivot cache. Check by opening Report Connections for the slicer: if the second PivotTable is not listed, it uses a different cache. Rebuild it by copying the first PivotTable, or create it from the same cache, then run the macro again.
Exercise
Write a one-line statement that prints the number of PivotTables connected to the slicer cache Slicer_Region.
Show answer
Debug.Print ActiveWorkbook.SlicerCaches("Slicer_Region").PivotTables.Count