EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Tips VBA Refresh Pivot Table
 

VBA Refresh Pivot Table

Ashwani Jaiswal
Article byAshwani Jaiswal
Madhuri Thakur
Reviewed byMadhuri Thakur

Excel VBA Refresh pivot table

Refresh Pivot Table in VBA

We usually create a pivot table when we need to create some kind of plots or charts, or we need to perform some analysis over it. By preparing the Pivot table, we can get the overall view and idea about what is actually inside the data. This is the best way to find way to get into data. And each time when we make any changes in the data, we need to refresh the pivot table as well. So that it will populate the updated data count as well. Refreshing any pivot table is very easy, but what if we have 10s of pivot tables in a single excel file which we need to update. So, instead of refreshing all the pivot tables one by one, we can directly refresh all the pivot tables in one go with the help of the VBA Refresh Pivot Table.

 

 

How to Use Refresh Pivot Table in Excel VBA?

Below are the different examples to use Refresh Pivot Table in Excel using VBA code.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

VBA Refresh Pivot Table – Example #1

For refreshing the pivot table, first, we need to create a pivot table. Below we have a data of 1000s line item by which we will be creating a pivot table.

VBA Refresh Pivot Table Example 1.1

In the above screenshot, we can see the last line at 1001 and this data has customer information against the quantity sold to them.

Now go to Insert menu tab and click on PivotTable option.

VBA Refresh Pivot Table Example 1.2

We will get a Create PivotTable box. From there, first, select the range of table which we want to include in a pivot table. Then select any location in the current sheet for Pivot table. We can choose New Worksheet as well.

Once done, click on Ok.

VBA Refresh Pivot Table Example 1.3

We will get the pivot table created. Now drag the required fields into different areas to get the actual pivot table. Here, we are dragging Customer name into ROWS and Quantity into COLUMNS as shown below.

VBA Refresh Pivot Table Example 1.4

This completes the creating of a PivotTable. But we need to update the pivot table after changing anything in Data, this could be done by simply refresh option from Right-click drop-down menu list as shown below.

VBA Refresh Pivot Table Example 1.5

But the same process can be automated through VBA as well. For this, we would need a Module. So,

Step 1: Go to Insert menu tab and select Module option from the drop-down list as shown below.

VBA Refresh Pivot Table Module

Step 2: In the newly opened Module, write the sub category of VBA Pivot Refresh or we can choose any name as per our choice.

Code:

Sub Pivot_Refresh2()

End Sub

VBA Refresh Pivot Table Example 1.6

Step 3: First, define a variable as PivotCache as shown below. PivotCache uses the Pivot cache not the data used for creating the Pivot table.

Code:

Sub Pivot_Refresh2()

Dim Table As PivotCache

End Sub

Example 1.7

Step 4: Now we will use the For-Each Loop. So open a For-Each loop as shown below.

Code:

Sub Pivot_Refresh2()

Dim Table As PivotCache
For Each

Next Table
End Sub

Example 1.8

Step 5: Now inside the For-End loop, we will write the condition where we will select the currently opened worksheet which has a Pivot table with PivotCache.

Code:

Sub Pivot_Refresh2()

Dim Table As PivotCache
For Each Table In ThisWorkbook.PivotCaches

Next Table
End Sub

Example 1.9

Step 6: Now we will use Refresh command assigning it the defined variable Table.

Code:

Sub Pivot_Refresh2()

Dim Table As PivotCache
For Each Table In ThisWorkbook.PivotCaches

Table.Refresh

Next Table
End Sub

Example 2.0

Step 7: This completes the code, now compile the code to file error by pressing F8 functional key. Now to test, whether the written code works on not, we have changed the quantity of Customer14 to 69.

 Example 1.11

Step 8: Now run the code. We will see the total count against Customer14 is updated to 2183, which is highlighted in Yellow color.

VBA Refresh Pivot Table Example 1.12

VBA Refresh Pivot Table – Example #2

There is another way to Refresh Pivot table through VBA. Before we move ahead, we can consider changing the name of a pivot table or we can use the default name as well. Let us try to give a new name to the Pivot table. For this, select the pivot table and go to Analyze menu tab as shown below.

In the first section of PivotTable Name, we can see, the default name as PivotTable1.

VBA Refresh PivotTable Example 2.1

Now we will change this name. Consider writing the new name as Customer Data as shown below.

VBA Refresh PivotTable Example 2.2

Step 1: After that, open a new module and write the sub category of VBA Refresh as shown below.

Code:

Sub Pivot_Refresh3()

End Sub

VBA Refresh PivotTable Example 2.3

Step 2: Now define a variable as PivotTable. Here, with the PivotTable, we will consider the complete source data.

Code:

Sub Pivot_Refresh3()

Dim Table As PivotTable

End Sub

Example 2.4

Step 3: Now use Set with defined variable and select the sheet which is currently opened.

Code:

Sub Pivot_Refresh3()

Dim Table As PivotTable
Set Table = ActiveSheet.

End Sub

VBA Refresh PivotTable Example 2.5

Step 4: Select the name of the Pivot table which we want to refresh along with the variable data type we have used as PivotTable.

Code:

Sub Pivot_Refresh3()

Dim Table As PivotTable
Set Table = ActiveSheet.PivotTables("Customer Data")

End Sub

VBA Refresh PivotTable Example 2.6

Step 5: At last, use the variable with RefreshTable function to execute it.

Code:

Sub Pivot_Refresh3()

Dim Table As PivotTable
Set Table = ActiveSheet.PivotTables("Customer Data")

Table.RefreshTable

End Sub

VBA Refresh PivotTable Example 2.7

Step 6: Let’s make some more changes in the data, to give the real visuals of applied code.

We have changed the count of Customer2 as 56. Now if we run the code, in the pivot table of Customer Name, there should be the changes in the sum of quantity for Customer2.

VBA Refresh PivotTable Example 2.8

Step 7: Now go to the VBA window and compile the code. If no error found then run it by clicking on play button which is below the menu bar as shown below. We will notice that a sum of quantity sold for Customer2 is now going up to 1724 by making the changes in the source table.

Example 2.9

By this, we can include more than one source data and create a different pivot table. And automating those pivot tables as well is easy as we just need to include the pivot table name and sheet where the table located.

Pros of VBA Refresh Pivot Table

  • It takes very less time in refreshing the pivot table by VBA code.
  • It is very easy to implement.

Things to Remember

  • We can add multiple data sources table and automate them by VBA code.
  • To see the changes happened, keep VBA window and Excel sheet parallel to each other.
  • It is better to name each and every pivot table if you are dealing and handling multiple data sources and pivot tables.
  • Code with pivot table name is easy to understand, track and locate.

Recommended Articles

This is a guide to VBA Refresh Pivot Table. Here we discuss how to use Excel VBA Refresh Pivot Table along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA FileSystemObject
  2. VBA Get Cell Value
  3. VBA Length of String
  4. VBA IsError
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

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

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

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

Download VBA Refresh Pivot Table Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW