Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Pivot Refresh: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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+F11 on Windows, or Fn+Option+F11 on 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

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). The editor opens in its own window.
  2. 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.
  3. Write the simplest refresh. Paste this into the module:
    Sub RefreshOnePivot()
        ActiveSheet.PivotTables(1).RefreshTable
    End Sub
    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.
  4. Target a PivotTable by name. Names are more reliable than index numbers because they survive reordering. Replace PivotTable1 with the name shown in PivotTable Analyze > PivotTable > PivotTable Name (the exact label varies by version):
    Sub RefreshByName()
        ActiveSheet.PivotTables("PivotTable1").RefreshTable
    End Sub
  5. Refresh every PivotTable on every worksheet. Loop through the workbook instead of hard-coding sheet names:
    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
    This is the safest general-purpose macro because it skips sheets that have no PivotTables.
  6. Refresh the underlying cache instead. If several PivotTables share one source range, refreshing the cache once is faster than refreshing each table:
    Sub RefreshCache()
        Dim pc As PivotCache
        For Each pc In ThisWorkbook.PivotCaches
            pc.Refresh
        Next pc
    End Sub
    Note that RefreshTable also refreshes the cache, so do not call both in the same routine unless you need to.
  7. Turn off screen updating for speed. On large models, wrap the loop:
    Application.ScreenUpdating = False
    ' ... refresh code ...
    Application.ScreenUpdating = True
    Always restore ScreenUpdating to True, even if an error occurs; otherwise Excel appears frozen.
  8. Run the macro. Press F5 with 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).
  9. Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx save silently strips the code.

Common Mistakes

  • Refreshing the wrong sheet. ActiveSheet depends on what is selected. If a user runs the macro from another tab, it fails or refreshes the wrong table. Use ThisWorkbook.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:D100 will 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 ScreenUpdating off. If the macro errors midway, Excel can look unresponsive. Add an error handler or reset it manually in the Immediate Window with Application.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_Open event 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 = xlCalculationManual before 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.