Sub topic 12
Refreshing and changing the data source
This chapter assumes you can create a PivotTable from a range or an Excel Table and place fields in the Rows, Columns and Values areas. You should also know what a flat source table is. Here you learn why a PivotTable shows old numbers after the data changes, and how to bring it up to date.
Why a PivotTable does not update on its own
A PivotTable does not read your cells live. When you create it, Excel stores a snapshot of the source data, called the pivot cache. The PivotTable draws from that cache. If you edit a value or add a row, the PivotTable keeps showing the old numbers until you refresh it.
Refreshing
Click any cell in the PivotTable, then use one of these:
- PivotTable Analyze > Refresh updates the selected PivotTable.
- Refresh All updates every PivotTable and data connection in the workbook.
- Right-click the PivotTable and choose Refresh.
To refresh when the file opens, open PivotTable Analyze > Options, go to the Data tab, and select Refresh data when opening the file.
For automation, this macro refreshes everything:
Sub RefreshAllPivots()
ThisWorkbook.RefreshAll
End SubWhen new rows do not appear
Refreshing only re-reads the range the PivotTable points to. Suppose the source is A1:E6 and you type a new record in row 7. The refresh cannot see row 7, because it is outside the range.
There are two fixes.
- Use an Excel Table as the source. A Table grows automatically when you add rows below it, and the PivotTable follows the Table by name. After you add rows, a refresh picks them up.
- Change the data source by hand. Click the PivotTable, choose PivotTable Analyze > Change Data Source, and select the larger range or the Table name.
Changing the source also lets you point the PivotTable at a different range or Table. Keep the same column headers, or fields you already placed may disappear.
What a refresh can change
- Column widths and formatting. By default, a refresh resets column widths. Clear Autofit column widths on update in PivotTable Options to keep yours.
- New items. A new region or product appears in the PivotTable after a refresh, unless a filter hides it.
- Removed items. An old item that no longer exists in the data can stay in the drop-down lists. Open PivotTable Options, go to the Data tab, and set Number of items to retain per field to None.
- Cells you typed over. Do not type inside the PivotTable body. A refresh can overwrite or move your entries.
Exercise
You add three new sales rows directly under a plain range that feeds a PivotTable, then click Refresh. The new rows do not appear. Give the cause and the best long-term fix.
Show answer
The PivotTable's source is a fixed range that does not include the new rows, so a refresh cannot see them. The best fix is to convert the data to an Excel Table and use the Table as the source (or use Change Data Source once to select it). The Table expands as rows are added, so future refreshes include them.