How to refresh all PivotTables in a workbook with a macro?

To refresh all PivotTables in an Excel workbook using a macro, you can create a simple VBA (Visual Basic for Applications) macro. Here’s a step-by-step guide to do this:

   Sub RefreshAllPivotTables()
       Dim wk As Workbook
       Dim ws As Worksheet
       Dim pt As PivotTable

       ' Set reference to the current workbook
       Set wk = ThisWorkbook

       ' Loop through each worksheet in the workbook
       For Each ws In wk.Worksheets
           ' Loop through each PivotTable in the worksheet
           For Each pt In ws.PivotTables
               pt.RefreshTable
           Next pt
       Next ws
   End Sub
  • Open the Excel Workbook: Open the Excel file in which you want to refresh all PivotTables.
  • Access the VBA Editor: Press `ALT + F11` to open the VBA editor.
  • Insert a New Module:
    • In the VBA editor, go to the toolbar and click on `Insert` > `Module`. This will create a new module window where you can write your macro.
  • Write the Macro Code:
    • Copy and paste the following VBA code into the module window:
  • Run the Macro:
    • Close the VBA editor and return to Excel.
    • Press `ALT + F8` to open the “Macro” dialog box.
    • Select `RefreshAllPivotTables` from the list of macros.
    • Click `Run`.

This macro loops through each worksheet in the workbook and refreshes all PivotTables it encounters. Make sure to save your workbook as a macro-enabled file with the `.xlsm` extension if you want to keep the macro for future use.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project