Excel VBA Pivot Refresh: Step-by-Step Guide
In this article
Refreshing a PivotTable from VBA means calling the PivotTable object's RefreshTable method (or the PivotCache's Refresh method) so the report re-reads its source data without you clicking anything. The shortest working version is one line: ActiveSheet.PivotTables(1).RefreshTable. Everything below explains how to target the right table, refresh every table at once, avoid the common errors, and keep the workbook responsive.
What You'll Need
- Excel on Windows or Mac with a workbook that already contains at least one PivotTable.
- The workbook saved as
.xlsm(macro-enabled) if you want the code to persist. - Access to the VBA editor: press
Alt+F11on Windows, orFn+Option+F11on a Mac. - Macro security set to allow macros in this workbook (File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; the equivalent setting lives in the Excel preferences on Mac). Labels vary slightly by version and platform.
Step-by-Step
- Open the VBA editor. Press
Alt+F11(Windows) orFn+Option+F11(Mac). The editor opens in its own window. - Insert a module. In the editor, choose Insert > Module. A blank code pane appears. Keeping your code in a standard module makes it callable from buttons and other macros.
- Write the simplest refresh. Paste this into the module:
This refreshes the first PivotTable on the active sheet. If the sheet has no PivotTable, it raises run-time error 1004, so make sure you are on the right sheet before running it.Sub RefreshOnePivot() ActiveSheet.PivotTables(1).RefreshTable End Sub - Target a PivotTable by name. Names are more reliable than index numbers because they survive reordering. Replace
PivotTable1with the name shown in PivotTable Analyze > PivotTable > PivotTable Name (the exact label varies by version):Sub RefreshByName() ActiveSheet.PivotTables("PivotTable1").RefreshTable End Sub - Refresh every PivotTable on every worksheet. Loop through the workbook instead of hard-coding sheet names:
This is the safest general-purpose macro because it skips sheets that have no PivotTables.Sub RefreshAllPivots() Dim ws As Worksheet Dim pt As PivotTable For Each ws In ThisWorkbook.Worksheets For Each pt In ws.PivotTables pt.RefreshTable Next pt Next ws End Sub - Refresh the underlying cache instead. If several PivotTables share one source range, refreshing the cache once is faster than refreshing each table:
Note thatSub RefreshCache() Dim pc As PivotCache For Each pc In ThisWorkbook.PivotCaches pc.Refresh Next pc End SubRefreshTablealso refreshes the cache, so do not call both in the same routine unless you need to. - Turn off screen updating for speed. On large models, wrap the loop:
Always restoreApplication.ScreenUpdating = False ' ... refresh code ... Application.ScreenUpdating = TrueScreenUpdatingtoTrue, even if an error occurs; otherwise Excel appears frozen. - Run the macro. Press
F5with the cursor inside the routine, or use Developer > Macros and pick the name. On the Developer tab, you can also assign the macro to a button (Insert > Shapes, right-click the shape, Assign Macro). - Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (
.xlsm). A plain.xlsxsave silently strips the code.
Common Mistakes
- Refreshing the wrong sheet.
ActiveSheetdepends on what is selected. If a user runs the macro from another tab, it fails or refreshes the wrong table. UseThisWorkbook.Worksheets("Report")when you know the sheet name. - Using the PivotTable name from the wrong place. The name in the Name Box is the cell range, not the PivotTable name. Use the PivotTable Name field on the Analyze tab.
- Forgetting that the source range may have grown. A PivotTable built on a fixed range such as
A1:D100will not pick up rows added below row 100. Convert the source to an Excel Table (Insert > Table) and base the PivotTable on that table name, or use a dynamic named range. - Leaving
ScreenUpdatingoff. If the macro errors midway, Excel can look unresponsive. Add an error handler or reset it manually in the Immediate Window withApplication.ScreenUpdating = True. - Saving as
.xlsx. The code disappears on save. Keep the file as.xlsm. - Refreshing a PivotTable whose source workbook is closed. External-source PivotTables can prompt for a password or fail if the linked file is unavailable. Open the source first, or refresh manually once to confirm it works.
Tips
- Name your PivotTables meaningfully (for example
ptSalesByRegion) so the code reads clearly and survives layout changes. - Put the refresh in the
Workbook_Openevent if you want fresh numbers every time the file opens. Keep it short so opening stays fast. - For dashboards, refresh the cache once rather than each table; shared caches make this much quicker.
- Use
Application.Calculation = xlCalculationManualbefore a large refresh and restore it afterwards to avoid recalculating formulas repeatedly. - If the source data is on another sheet that is filtered or hidden, remember that PivotTables read the whole range, not just visible rows.
- Keep a manual refresh button on the sheet for users who do not run macros; assign the same macro to a shape.
Troubleshooting
Run-time error 1004 "Unable to get the PivotTables property". The active sheet has no PivotTable, or the name is wrong. Check the sheet and the PivotTable Name field.
Run-time error 5 or "Invalid procedure call". Usually a typo in the PivotTable name or a name that no longer exists after a copy/paste. Re-check the name.
Macro runs but numbers do not change. The source range has not grown, or the PivotTable is based on a static range. Convert the source to a Table and rebuild the PivotTable, or extend the range.
Excel freezes or takes minutes. Large data plus many PivotTables. Turn off ScreenUpdating, switch calculation to manual, and refresh the cache once instead of each table.
"Cannot open PivotTable source file". The source workbook is closed or moved. Open it, or update the connection via PivotTable Analyze > Change Data Source.
Macro missing after reopening. The file was saved as .xlsx. Save as .xlsm.
Macros blocked. Check Trust Center macro settings, or unblock the file (right-click the file > Properties > Unblock on Windows).
Frequently Asked Questions
Can I refresh all PivotTables with one line?
Not reliably with a single built-in call. ThisWorkbook.RefreshAll refreshes data connections and PivotCaches, but the safest explicit approach is a loop over each worksheet's PivotTables as shown above.
Does RefreshTable update the PivotCache?
Yes. RefreshTable refreshes the PivotTable and its cache. If several tables share a cache, refreshing one updates the data for all of them, though each table's layout still needs its own refresh to redraw.
Why does my macro fail when run from a button?
Buttons run the macro against whatever sheet is active. If the button sits on a different sheet from the PivotTable, use an explicit worksheet reference instead of ActiveSheet.
Can I refresh a PivotTable on a protected sheet?
Only if the protection allows it. Unprotect the sheet in code (ws.Unprotect "password"), refresh, then re-protect. Storing passwords in code is insecure; use it only for internal workbooks.
Will the macro work on Excel for Mac?
Yes, the object model is the same. The shortcut to open the editor differs and some Trust Center labels are in different places, but RefreshTable and PivotCaches behave the same.
How do I refresh on a schedule?
Use the Workbook_Open event, or Application.OnTime to run the macro at set intervals while the workbook is open. Excel must remain open for either to fire.