VBA macros · Data
VBA macro to refresh all pivot tables in Excel
Refresh every pivot table on every sheet of a workbook with one macro, without refreshing queries and connections, and count how many were updated.
Pivot tables do not update by themselves when the source data changes. Data › Refresh All works, but it also refreshes every query and external connection, which can be slow or ask for passwords. This macro refreshes only the pivot tables, on every sheet of the active workbook, and tells you how many it updated.
Option Explicit
Sub RefreshAllPivotTables()
Dim ws As Worksheet
Dim pt As PivotTable
Dim n As Long
Dim failed As String
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
On Error Resume Next
pt.RefreshTable
If Err.Number <> 0 Then
failed = failed & vbLf & ws.Name & " / " & pt.Name & ": " & Err.Description
Else
n = n + 1
End If
On Error GoTo 0
Next pt
Next ws
Application.ScreenUpdating = True
If Len(failed) > 0 Then
MsgBox n & " pivot table(s) refreshed. Problems:" & failed, vbExclamation
Else
MsgBox n & " pivot table(s) refreshed.", vbInformation
End If
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickRefreshAllPivotTablesand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
Each worksheet has a PivotTables collection. Two nested For Each loops visit every pivot table on every sheet. pt.RefreshTable re-reads the source data and redraws the table, the same as right-clicking it and choosing Refresh.
A single refresh can fail, for example when the source range was deleted or the sheet is protected. Instead of stopping, the macro catches the error for that one pivot table, notes the sheet and pivot name, and carries on. The summary at the end lists the problems. Screen updating is off during the loop so the screen does not redraw after every table; see Make your macros fast: speed optimization in VBA.
Pivot tables built on the same source share a pivot cache, a stored copy of the data. Refreshing one of them updates the cache and therefore all of them, so with many pivot tables on one source the loop does a little more work than strictly necessary. For most workbooks it does not matter. Pivot tables in VBA are covered in depth in Pivot tables with VBA: create, filter and refresh.
Change it
For workbooks with many pivot tables on a few sources, refreshing each cache once is faster:
Sub RefreshAllPivotCaches()
Dim pc As PivotCache
For Each pc In ActiveWorkbook.PivotCaches
pc.Refresh
Next pc
End SubTo refresh the pivot tables automatically whenever someone opens the sheet that holds them, put Private Sub Worksheet_Activate() with Me.PivotTables(1).RefreshTable in that sheet's code module; see Events: macros that run by themselves. To refresh everything including Power Query queries, use ActiveWorkbook.RefreshAll; queries that run in the background may still be loading when the macro ends, so turn off Enable background refresh in the query properties if later code depends on them.
What can go wrong
If a pivot table's source is a fixed range such as A1:F500, new rows added below row 500 are not included after a refresh. Turn the source into an Excel table (Ctrl+T) and point the pivot table at the table; then it grows automatically. See Excel tables in VBA: working with ListObjects.
Related: Pivot tables with VBA: create, filter and refresh, VBA macro to show a progress bar in the Excel status bar.