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.

Windows + Mac

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.

VBA
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 Sub
Refresh every pivot table in the active workbook

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick RefreshAllPivotTables and click Run.
  5. 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:

VBA
Sub RefreshAllPivotCaches()
    Dim pc As PivotCache
    For Each pc In ActiveWorkbook.PivotCaches
        pc.Refresh
    Next pc
End Sub
Refresh each pivot cache once

To 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.

Watch out. A PivotTable report cannot overlap another PivotTable report means a pivot table would grow into a neighbouring one after the refresh. Move one of them further away. On protected sheets, refreshing is blocked unless the sheet was protected with Use PivotTable & PivotChart allowed.
Tip. Works on Windows and Mac. Pivot tables based on the Data Model or on external databases refresh too, but may ask for credentials, which a macro cannot supply for you.

Related: Pivot tables with VBA: create, filter and refresh, VBA macro to show a progress bar in the Excel status bar.

More vba macros