In Excel, you can refresh a pivot table automatically using macro code. There are multiple way to refresh the excel pivot table. We will see one by one to execute in excel workbook.
To refresh multiple pivot table in excel workbook. follow the below methods to execute automatically.
Copy the any one of the below code and paste it in your excel workbook where it required to refresh the pivot table.
Method 1:
Dim WSheet as WorkSheet, Pivot as PivotTable
For Each WSheet in ThisWorkbook.WorkSheets
For Each Pivot in WSheet.PivotTables
Pivot.RefreshTable
Next
Next
For Each WSheet in ThisWorkbook.WorkSheets
For Each Pivot in WSheet.PivotTables
Pivot.RefreshTable
Next
Next
Method 2:
ThisWorkbook.RefreshAll
Method 3:
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
Dim pt as PivotTable
For Each ws In Activeworkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
Method 4:
Activeworkbook.RefreshAll
To refresh single pivot table follow the below method to execute in excel.
Method 1:
Sub Pivot_Table_Macro()
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("My_Pivot")
pt.RefreshTable
End Sub
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("My_Pivot")
pt.RefreshTable
End Sub