Refreshing all pivot tables in Excel using VBA ensures your data summaries update automatically when the source data changes, saving time and reducing manual errors. This method is especially useful when working with large dashboards or reports that pull from multiple data sources. Before you begin, ensure your workbook contains at least one pivot table and that the source data is properly structured in a table or range with headers. No add-ins or external tools are required—this uses Excel’s built-in VBA engine.

What You'll Need

  • Microsoft Excel 2010 or later (Windows or macOS)
  • A workbook containing one or more pivot tables
  • Access to the VBA editor (enabled via Developer tab or Alt+F11)
  • Source data that is not protected or locked

Step-by-Step

  1. Open the Excel workbook that contains your pivot tables. Make sure the source data for each pivot table is updated or ready to be refreshed.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. If the Developer tab is not visible, enable it via File > Options > Customize Ribbon and check the Developer box.
  3. In the VBA editor, find the Project Explorer window on the left. If it’s not visible, press Ctrl + R. Right-click on your workbook’s name under VBAProject (e.g., VBAProject (YourWorkbook.xlsx)) and select Insert > Module.
  4. In the new module window, paste the following VBA code:
    Sub RefreshAllPivotTables()
        Dim ws As Worksheet
        Dim pt As PivotTable
        
        For Each ws In ActiveWorkbook.Worksheets
            For Each pt In ws.PivotTables
                pt.RefreshTable
            Next pt
        Next ws
    End Sub
  5. Save your workbook as a macro-enabled file (.xlsm). Go to File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown, and click Save.
  6. Close the VBA editor by clicking the X or pressing Alt + Q.
  7. Return to Excel. Press Alt + F8 to open the Macro dialog box. Select RefreshAllPivotTables and click Run.
  8. Wait a few seconds while Excel refreshes each pivot table. Check each pivot table to confirm the data has updated. If the source data has changed, the pivot tables will reflect the new values.
  9. (Optional) To make this run automatically when the workbook opens, go back to the VBA editor, double-click ThisWorkbook under your project, and paste this code:
    Private Sub Workbook_Open()
        RefreshAllPivotTables
    End Sub

Common Mistakes

  • Running the macro on a .xlsx file instead of .xlsm—Excel will block macros and the code won’t execute. Always save as macro-enabled.
  • Assuming all pivot tables refresh instantly. Some pivot tables connected to external databases or Power Query may take longer or require additional refresh steps.
  • Using RefreshAll instead of RefreshTable—RefreshAll only refreshes pivot tables and external data connections, but may skip certain cache updates. RefreshTable is more reliable per pivot.
  • Not ensuring the source data range hasn’t shifted. If you added or deleted rows/columns in the source, the pivot table’s data source reference might break. Use Excel Tables (Ctrl+T) for source data to avoid this.
  • Running the macro when the source data is still being edited. Always finish editing source data before triggering the refresh.

Tips

  • Use Excel Tables (Insert > Table) for your source data. This ensures dynamic range expansion, so new rows are automatically included in pivot tables without manual range adjustments.
  • Group related pivot tables by worksheet and name them clearly (e.g., “Pivot_Sales”, “Pivot_Inventory”) to make troubleshooting easier.
  • Add a status message to your macro to confirm completion: after the last Next ws, add MsgBox "All pivot tables refreshed.", vbInformation.
  • For workbooks with external connections (e.g., SQL, Access), refresh the connections first using ActiveWorkbook.RefreshAll before looping through pivot tables.
  • Test your macro on a copy of your workbook first. If the source data is corrupted or linked to a broken external file, refreshing all pivots may cause errors.
  • Use the Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end of your macro to speed up execution and prevent screen flicker.

Troubleshooting

  • Macro doesn’t run or says "Macro not found": Confirm the file is saved as .xlsm. Check that macros are enabled: File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros (for testing only).
  • Some pivot tables don’t update: Right-click each pivot table and select "PivotTable Options" > Data > check "Refresh data when opening the file". Also verify the data source range still exists and hasn’t been deleted.
  • Runtime error 9: Subscript out of range: This happens if a worksheet has no pivot tables but the code tries to access one. The provided code handles this safely, but if you modified it, ensure you’re not referencing a non-existent worksheet.
  • Slow refresh times: Disable automatic calculation before running the macro: add Application.Calculation = xlCalculationManual at the start and Application.Calculation = xlCalculationAutomatic at the end. This prevents Excel from recalculating formulas during refresh.
  • Power Pivot tables don’t refresh: This code only refreshes standard pivot tables. For Power Pivot models, use ActiveWorkbook.Model.Refresh in addition to the loop.
  • Permission errors: If source data is on a network drive or SharePoint, ensure you have read/write access. Try opening the file locally if possible.

Frequently Asked Questions

Can I refresh pivot tables without using VBA?

Yes. Select any pivot table, go to the PivotTable Analyze tab, and click Refresh. To refresh all at once, press Ctrl+Alt+F5. But this requires manual action each time.

Why does my pivot table still show old data after running the macro?

The source data may not have changed, or the pivot table’s data source range is incorrect. Check the pivot table’s Data Source under PivotTable Analyze > Change Data Source.

Will this work with pivot tables linked to external databases?

Yes, but those connections may require additional authentication or network access. The VBA code triggers the refresh, but the external connection must be active and accessible.

Can I refresh only certain pivot tables, not all?

Yes. Modify the VBA code to loop through specific sheets or name the pivot tables you want to refresh. For example: Worksheets("Sheet1").PivotTables("PivotTable1").RefreshTable.

What if I get an "Object required" error?

This usually means a worksheet has no pivot tables. The provided code avoids this by checking each worksheet’s PivotTables collection before looping. If you modified the code, ensure you didn’t remove the For Each pt In ws.PivotTables check.

Does this work on Mac?

Yes, Excel for Mac supports VBA and the same code works. Ensure the Developer tab is enabled via Excel > Preferences > Ribbon & Toolbar and check Developer.