OzGrid

Refresh Pivot Table via VBA

< Back to Search results

 Category: [Excel]  Demo Available 

Refresh Pivot Table via VBA

 

Refresh Pivot Table via VBA/Update Pivot Table With VBA

Got any Excel/VBA Questions? Free Excel Help

Refresh Pivot Tables

Those of you that use pivot tables will be well aware of their power. However, one draw-back can be that you can only refresh the PivotTables automatically by setting it refresh on open via the PivotTable Options. Lets look at some ways we can refresh all, or chosen Pivot Tables.

The code below here can be called (Run) via the Worksheet_Activate Event. To get to the Private Module of any Worksheet right click on the sheet name tab and choose View Code. In you would place code like shown below;

Private Sub Worksheet_Activate()

    Run "PivotMacro"

End Sub

Refresh a Single Pivot Table

Sub PivotMacro()

Dim pt As PivotTable



    Set pt = ActiveSheet.PivotTables("MyPivot")

    pt.RefreshTable

End Sub

Refresh all Pivot Tables in a Worksheet

Sub AllWorksheetPivots()

Dim pt As PivotTable



    For Each pt In ActiveSheet.PivotTables

        pt.RefreshTable

    Next pt

    

End Sub

Refresh Chosen Pivot Tables in a Worksheet

Sub ChosenPivots()

Dim pt As PivotTable



    For Each pt In ActiveSheet.PivotTables

    

        Select Case pt.Name

            Case "PivotTable1", "PivotTable4", "PivotTable8"

                pt.RefreshTable

            Case Else

        End Select

    Next pt

    

End Sub

Refresh All Chosen Pivot Tables in a Workbook

Sub AllWorkbookPivots()

Dim pt As PivotTable

Dim ws As Worksheet



    For Each ws In ActiveWorkbook.Worksheets

    

        For Each pt In ws.PivotTables

                    pt.RefreshTable

        Next pt

        

    Next ws

    

End Sub
 

See also:

Excel VBA: Hide/Show Pivot Table Field Items
Printing PivotTables & PivotCharts

 

See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)