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
Sub PivotMacro()
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("MyPivot")
pt.RefreshTable
End Sub
Sub AllWorksheetPivots()
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Next pt
End Sub
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
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
| 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.