WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Refresh Pivot Table

VBA Refresh Pivot Table

Excel VBA Refresh Pivot Table

When we insert a pivot table in the sheet, once the data changes pivot table data does not change itself we need to do it manually but in VBA there is a statement to refresh pivot table which is expression.refreshtable, using this we can refresh the pivot table by referencing to the worksheet consisting it or we can refer to the entire pivot tables in the worksheets and refresh them all at once.

The pivot table is vital in analyzing the huge amount of data. It helps right from analyzing, summarizing, as well as making useful data interpretation. However, one of the problems with this pivot table is it will not automatically get refreshed if there is any change in the source data. The user has to refresh the pivot table by going to the particular pivot table every time when there is a change. But say goodbye to the manual process because here we have the method to refresh the pivot table as soon as you make any change in the pivot table.

VBA Refresh Pivot Table

How to Auto Refresh Pivot Table Data VBA Code?

The only time the pivot table needs to be updated is whenever there is any change in the source data of the pivot table we are referring to.

For example, look at the below data and pivot table.

Auto Refresh Pivot Tables

Now I will change the numbers in source data, i.e., from A1 to B17.

Auto Refresh Pivot Tables 1

In cell B9, I have to change the value from 499 to 1499, i.e., 1000 increase in the data but if you look at the pivot still shows the result as 4295 instead of 5295. I have to manually refresh my pivot table to update the pivot table.

To overcome this issue, we need to write a simple excel macro code to refresh the pivot table whenever there is any change in the source data.

You can download this VBA Refresh Pivot Table Excel Template here – VBA Refresh Pivot Table Excel Template

#1 – Simple Macro to Refresh All Table

Step 1: Change Event of the Datasheet

We need to trigger the change event of the datasheet. In the visual basic editor, double click on the datasheet.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

Refresh Pivot Tables Example 1

Once you double-click on the sheet, select “Worksheet” and select the event as “Change.”

Refresh Pivot Tables Example 1-1

You will see an auto sub procedure opened as Worksheet_Change(ByVal Target As Range)

Refresh Pivot Tables Example 1-2

Step 2: Use Worksheet Object

Refer to the datasheet by using the Worksheets object.

Example 1-3

Step 3: Refer Pivot Table by Name

Refer to the pivot table name by the name of the pivot table.

Example 1-4

Step 4: Use the Refresh Table Method

Select the method as “Refresh Table.”

Example 1-5

Now, this code will refresh the pivot table “PivotTable1” whenever there is any change in the source datasheet. You can use the below code. You just have to change the pivot table name.

Code:

Private Sub Worksheet_Change(ByVal Target As Range)

  Worksheet("Data Sheet").PivotTables("PivotTable1").RefreshTable

End Sub

#2 – Refresh All Pivot Tables of the Same Worksheet

If you have many pivot tables in the same worksheet, you can refresh all the pivot tables in a single click itself. Use the below code to refresh all the pivot tables in the sheet.

Code:

Sub Refresh_Pivot_Tables_Example1()
  Worksheets("Data Sheet").Select

  With ActiveSheet
    .PivotTables("Table1").RefreshTable
    .PivotTables("Table2").RefreshTable
    .PivotTables("Table3").RefreshTable
    .PivotTables("Table4").RefreshTable
    .PivotTables("Table5").RefreshTable
  End With

End Sub

You need to change the name of the worksheet and pivot table names as per your worksheet details.

#3 – Refresh All Tables in the Workbook

It is highly unlikely we have all the pivot tables on the same worksheet. Usually, for each report, we try to add separate pivot tables in separate sheets. In these cases, we cannot keep writing the code for each pivot table to be refreshed.

So, what we can do is with a single code using loops, we can loop through all the pivot tables on the workbook and refresh them with a single click of the button.

The below code will loop through each pivot table and refresh them.

Code 1:

Sub Refresh_Pivot_Tables_Example2()

  Dim PT As PivotTable

  For Each PT In ActiveWorkbook.PivotTables
    PT.RefreshTable
  Next PT

End Sub

Code 2:

Sub Refresh_Pivot_Tables_Example3()

  Dim PC As PivotCache

  For Each PC In ActiveWorkbook.PivotCaches
    PC.Refresh
  Next PT

End Sub

Both the codes will do the refreshing of pivot tables.

If you want the pivot table to be refreshed as soon as there is any change in the datasheet of the pivoting sheet, you need to copy and paste the above codes to the Worksheet Change event in that workbook.

#4 – Avoid Loading Time by using Worksheet Deactivate Event

When we use the “Worksheet Change” event, it keeps refreshing even when there is no change in the data source, but if any change happens in the worksheet.

Even if you enter one single dot in the worksheet, it tries to refresh the pivot table. So to avoid this, we can use the “Worksheet Deactivate” method instead of the “Worksheet Change” method.

Worksheet Deactivate Event

Deactivate the event updates on the pivot table when moving from one sheet to another sheet.

Recommended Articles

This has been a guide to VBA Refresh Pivot Table. Here we learn how to auto-refresh pivot tables using excel VBA Macros Code along with examples and a downloadable template. Below are some useful excel articles related to VBA –

  • VBA ME
  • VBA COUNTA Worksheet Function
  • Pivot Table using VBA
  • Pivot Table From Multiple Sheets
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA Refresh Pivot Table Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More