Sub topic 3
Inserting a Slicer
This chapter assumes you can build a basic PivotTable from a range or an Excel Table. It also assumes you can drag fields into the Rows, Columns, Values, and Filters areas, and that you know the ribbon's contextual tabs. A slicer is a filter control that sits on the worksheet as a panel of buttons. This chapter covers how to insert one, what Excel creates when you do, and the errors you are most likely to hit.
What you need before you insert
A slicer attaches to a PivotTable, so you need an existing PivotTable. The field you slice by does not have to be placed in the Rows, Columns, Values, or Filters area. Any field in the PivotTable's source data can be used.
The workbook must be saved in a format that supports slicers. This means .xlsx, .xlsm, or .xlsb. In the older .xls format, the Slicer command is not available.
Two quick checks will save time later:
- Click a cell inside the PivotTable. The PivotTable Analyze tab appears only when a PivotTable cell is selected.
- Check that the field's values are clean. A Region field with both "North" and "North " (a trailing space) produces two separate buttons.
Inserting a slicer from the ribbon
Follow these steps for a PivotTable built from sales data.
- Click any cell inside the PivotTable.
- Open the PivotTable Analyze tab. In recent versions this tab may be named PivotTable Analyze. In Excel 2013 and 2016 it is Analyze.
- In the Filter group, click Insert Slicer.
- In the Insert Slicers dialog box, tick one or more fields, for example Region.
- Click OK.
Excel places one slicer per ticked field on the sheet. Each slicer shows one button for every unique value in the field. A slicer is a floating object. It sits above the cells and does not occupy any grid cells, so it can cover your data if you do not move it.
You can also use Insert > Filters > Slicer on the Insert tab. Both routes open the same dialog box.
What Excel creates behind the scenes
Inserting a slicer creates two objects, and the difference between them explains most later behavior.
- The slicer is the visible panel with buttons. It has a name such as
Region. - The slicer cache is a hidden object that stores which items are selected. It has a name such as
Slicer_Region.
The slicer cache is what connects to PivotTables. The slicer only displays the cache's state. This is why one slicer cache can drive several PivotTables, which a later chapter covers under Report Connections.
The first slicer for a field gets the cache name Slicer_ followed by the field name. Spaces become underscores, so a field named "Sales Rep" gives Slicer_Sales_Rep. If you insert a second slicer for the same field on another PivotTable that uses a different pivot cache, Excel numbers the name, for example Slicer_Region1. You need these cache names when you write VBA.
Using the slicer after insertion
When the slicer is selected, the Slicer contextual tab appears. It holds the Slicer Settings, Report Connections, size, style, and arrangement commands. Later chapters cover most of these. For now, test the slicer:
- Click one button, for example East. The PivotTable shows only East rows. Selected buttons are highlighted, and unselected ones are dimmed in the default style.
- Click the filter icon with the red cross at the top right of the slicer to clear the filter.
Selection behavior in more detail comes in the next chapter.
Slicers on an Excel Table
You can also insert a slicer for an Excel Table without any PivotTable. Click inside the Table, open Table Design, and choose Insert Slicer. The slicer then filters the Table rows directly. This chapter focuses on PivotTables, but the dialog and the objects are the same, and the slicer cache is tied to the Table instead.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Insert Slicer is greyed out | No PivotTable cell is selected, or the file is in `.xls` format | Click inside the PivotTable, or save as `.xlsx` |
| The field you want is missing from the dialog | The field is not in the source data, or it is a calculated field | Add the column to the source data and refresh. Calculated fields cannot be sliced |
| Slicers pile up in one spot | Several were created at once | Drag them apart, or use the Slicer tab's size and position boxes |
| Buttons show duplicates such as "North" and "North " | Inconsistent text in the source | Clean the source with TRIM, then refresh |
| A new item does not appear in the slicer | The PivotTable has not been refreshed | Right-click the PivotTable and choose Refresh |
| A slicer covers part of the report | Slicers float above cells | Move it, or set its placement in Size and Properties |
The refresh row deserves a note. A slicer reads its buttons from the pivot cache. New values you add to the source data show up only after you refresh the PivotTable. If you use a plain range as the source, new rows below the range are also not included until you change the data source. Building the PivotTable from an Excel Table avoids this problem.
Positioning and placement properties
Once a slicer exists, you can control how it behaves when you resize or move cells. Right-click the slicer and choose Size and Properties. Under Properties you will find three placement options:
- Move and size with cells makes the slicer follow the cells it sits over. It stretches if you resize those columns.
- Move but don't size with cells keeps the slicer the same size and moves it with the cells.
- Don't move or size with cells pins the slicer to a fixed place on the sheet.
For dashboards, the third option is usually the safest, because filtering can change the number of rows in a PivotTable and push cells around. A slicer set to move with cells may then drift.
Inserting a slicer with VBA
If you create the same slicer often, a macro can do it. This macro builds a slicer cache for the Region field of the first PivotTable on the active sheet and 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 Add2 call creates the slicer cache, and Slicers.Add creates the visible slicer. This matches the two-object model described earlier. The last four numbers are the top, left, width, and height of the slicer in points. Running the macro twice on the same field raises an error, because a slicer named RegionSlicer already exists. Delete the existing slicer first, or use a different name.
Summary of the procedure
- Select a cell in the PivotTable.
- Choose Insert Slicer from the PivotTable Analyze tab.
- Tick the fields you want to filter by.
- Move the slicers so they do not overlap the data or each other.
- Click buttons to test the filter, and clear it with the red-cross icon.
- Set the placement property if the report layout changes when the data is filtered.
Exercise
You tick Region, Product, and Sales Rep in the Insert Slicers dialog and click OK, but you see only one slicer on the sheet. Did Excel fail to create the other two? How do you check, and what hidden object does each slicer use?
Show answer
Excel almost certainly created all three. New slicers are stacked in the same position, so the top one hides the others. Drag the top slicer aside, or use the Selection Pane (Home > Find & Select > Selection Pane) to list every object on the sheet. Each slicer has its own slicer cache, named for example SlicerRegion, SlicerProduct, and SlicerSalesRep. The cache stores the selected items and connects the slicer to the PivotTable.
Exercise
A colleague adds a new region, "Central", to the source Table. Your Region slicer does not show it. What is the cause, and what is the fix?
Show answer
The slicer reads its buttons from the pivot cache, and the cache still holds the old data. Refresh the PivotTable (right-click it and choose Refresh, or use Data > Refresh All). If the source is a plain range instead of an Excel Table, also check that the PivotTable's data source includes the new rows.