Sub topic 3
Inserting a slicer
This chapter assumes you can build a PivotTable from a range or an Excel Table and drag fields into the Rows, Columns, Values, and Filters areas. You should also be comfortable with the ribbon and its contextual tabs. A slicer is a set of buttons that filters a PivotTable. Here you will add one, read what the ribbon shows you, and then do the same thing in code.
Before you start
Click any cell inside the PivotTable. The PivotTable Analyze tab (named Analyze in some versions) appears only while a cell in the PivotTable is selected. If you click outside the PivotTable, the slicer commands disappear.
The field you slice by does not need to be in the PivotTable layout. You can slice by Region while Region is in none of the four areas. The slicer filters the data either way.
Insert a slicer with the ribbon
- Select a cell inside the PivotTable.
- Go to PivotTable Analyze and choose Insert Slicer.
- In the Insert Slicers dialog, tick one or more fields. Each ticked field gets its own slicer.
- Click OK.
Excel places one slicer for each field you ticked. The slicers are stacked near the top left of the sheet, so they often overlap each other and the data. Drag each slicer by its border to move it.
You can also use Insert > Slicer. This command works on the PivotTable that contains the selected cell.
What happens when you click a button
Each button is one item from the field. Clicking a button filters the PivotTable to that item. Selected buttons are highlighted, and unselected buttons are greyed out. The next chapter covers multiple selection and clearing the filter in detail.
Insert a slicer with VBA
The ribbon steps can be recorded as a macro. This version 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 SubThe arguments to Slicers.Add are the sheet, an optional slicer destination, the slicer name, the caption, and then the top, left, width, and height in points. Placing the slicer at a fixed position avoids the overlap you get from the ribbon.
Common errors
- Insert Slicer is greyed out. No cell in a PivotTable is selected. Click inside the PivotTable first.
- The macro fails on the second run. A slicer cache or slicer with that name already exists. Delete the old slicer or use a different name.
- A field is missing from the dialog. The dialog lists the fields of the selected PivotTable's source. Check that the column has a header and that you refreshed the PivotTable after changing the source.
Exercise
You select a cell in a PivotTable, open Insert Slicers, and tick Region and Product. How many slicers does Excel create, and what name does the slicer cache for Region have by default?
Show answer
Excel creates two slicers, one for Region and one for Product. The slicer cache for Region is named Slicer_Region.
Exercise
Why does the Insert Slicer command disappear from the ribbon when you click a blank cell outside the PivotTable?
Show answer
The PivotTable Analyze tab is a contextual tab. Excel shows it only while the selected cell is inside a PivotTable.