Table of Contents
Pivot tables are a powerful tool in Microsoft Excel that allows users to summarize and analyze large amounts of data. However, as the underlying data changes, it is necessary to refresh pivot tables to reflect the updated information. This can be done manually, but in cases where there are multiple pivot tables or frequent updates, it is more efficient to use VBA code. This involves using the “RefreshTable” method to update the pivot table’s data source and “Update” method to recalculate and display the new data. Examples of VBA code for refreshing pivot tables include using a button or a timer to automatically refresh the tables at specified intervals, or using a macro to refresh all pivot tables in a workbook. These methods provide a streamlined and efficient way to keep pivot tables up to date with the latest data changes.
Refresh Pivot Tables in VBA (With Examples)
You can use the following methods to refresh pivot tables in Excel using VBA:
Method 1: Refresh One Specific Pivot Table
Sub RefreshPivotTable()
Sheet1.PivotTables("PivotTable1").Refreshtable
End Sub
This particular macro will refresh only the values in the pivot table called PivotTable1 in Sheet1 of the workbook.
Method 2: Refresh All Pivot Tables in Workbook
Sub RefreshAllPivotTables()
ThisWorkbook.RefreshAll
End SubThis particular macro will refresh the values in every pivot table in the entire workbook.
The following examples show how to use each of these methods in practice.
Example 1: Refresh One Specific Pivot Table
Suppose we have created one pivot table from a dataset in Excel:

When we are on the PivotTable Analyze tab along the top ribbon in Excel, we can see in the top left corner that this pivot table is named PivotTable1.
Suppose we change the last value in the points column of the dataset from 22 to 200:

We can create the following macro to refresh the values in this specific pivot table:
Sub RefreshPivotTable()
Sheet1.PivotTables("PivotTable1").Refreshtable
End SubWhen we run this macro, the values in the pivot table are automatically updated:

Example 2: Refresh All Pivot Tables in Workbook
Suppose we have created two pivot tables from a dataset in Excel:

The first pivot table shows the sum of points by team and position.
The second pivot table shows the average of points by team and position.
Suppose we change the last value in the points column of the dataset from 22 to 200.
We can create the following macro to refresh the values in all pivot tables in the entire workbook:
Sub RefreshAllPivotTables()
ThisWorkbook.RefreshAll
End SubWhen we run this macro, the values in both pivot tables are automatically updated:

Cite this article
stats writer (2024). How can we refresh pivot tables in VBA? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-refresh-pivot-tables-in-vba-can-you-provide-some-examples/
stats writer. "How can we refresh pivot tables in VBA? Can you provide some examples?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-we-refresh-pivot-tables-in-vba-can-you-provide-some-examples/.
stats writer. "How can we refresh pivot tables in VBA? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-refresh-pivot-tables-in-vba-can-you-provide-some-examples/.
stats writer (2024) 'How can we refresh pivot tables in VBA? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-refresh-pivot-tables-in-vba-can-you-provide-some-examples/.
[1] stats writer, "How can we refresh pivot tables in VBA? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can we refresh pivot tables in VBA? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
