Excel vba pivotcache refresh

This tutorial explains how to use Excel VBA to refresh all Pivot Table or Pivot Cache, and difference between PivotCache.Refresh and PivotTable.RefreshTable

You may also want to read:

Excel VBA refresh closed workbook

Excel automatically refresh pivot table

Understanding difference between Pivot Table and Pivot Cache

To start with this topic, you should note the relationship and difference between refresh Pivot Table and Pivot Cache.

When you create a Pivot Table, Pivot Cache is created to store the source data in cache memory to speed up Pivot Table. Afterwards, Pivot Table will use the Pivot Cache as data source. If you are using multiple Pivot Tables using the same data source, all Pivot Tables use the same Pivot Cache in order to minimize your file size.

When you refresh a Pivot Table (right click on the Pivot Table > Refresh), you are refreshing Pivot Cache. Because Pivot Cache is refreshed, Pivot Table also gets refreshed. Therefore, you refresh all Pivot Tables that uses the same Pivot Cache by refreshing just one Pivot Table.

If you want Pivot Table to be independent, you can use Name Range to name the data source to other names (e.g Table1, Table2), and assign different name range to different Pivot Tables.

Read the below three VBA Methods regarding Refresh Pivot Table.

PivotTable.RefreshTable Method – Refreshes the PivotTable report from the source data. Returns True if it’s successful.

PivotTable.PivotCache().Refresh() Method – Updates the cache of the PivotTable object

Although VBA provides two different Methods, all of them will refresh Pivot Cache in the end, so it doesn’t matter which one to use. Therefore in the following sections, I will only show how to refresh Pivot Table.

Refresh all Pivot Table or Pivot Cache

To refresh all Pivot Table, there are two ways.

1) Using RefreshAll Method

The below action of VBA code is same as clicking Data > Refresh All

ActiveWorkbook.RefreshAll

This method is very simple and refreshes all Pivot Tables in the workbook, however, it also refreshes all other external connections.

2) Loop through individual Pivot Table and use RefreshTable Method

RefreshTable Method is same as right click on a Pivot Table and click “Refresh”. Although the below code looks like only refresh only one Pivot Table for each loop, but as I noted previously, refreshing one Pivot Table would refresh all other Pivot Tables using the same Pivot Cache.

Public Sub refreshPT()
    Dim ws As Worksheet
    Dim pt As PivotTable
    For Each ws In ThisWorkbook.Worksheets
        For Each pt In ws.PivotTables
            pt.RefreshTable
        Next
    Next
End Sub

Excel VBA refresh Pivot Table of specific name

The below code shows how to refresh Pivot Table of specific name (as noted above, this will also refresh Pivot Tables using the same Pivot Cache).

ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh

OR

ActiveSheet.PivotTables("PivotTable1").RefreshTable

You can also loop through each name  in the workbook.

Public Sub refreshPivot()
    Dim ws As Worksheet
    Dim pt As PivotTable
    For Each ws In ThisWorkbook.Worksheets
        For Each pt In ws.PivotTables
            Select Case pt.Name
                Case "PivotTable1", "PivotTable2"
                    pt.RefreshTable
            End Select
        Next
    Next
End Sub

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

PivotCache.Refresh method (Excel)

vbaxl10.chm227080

vbaxl10.chm227080

excel

Excel.PivotCache.Refresh

2833d199-342c-9e2e-d1f8-88c33a74bac6

05/03/2019

medium

PivotCache.Refresh method (Excel)

Causes the specified PivotCache to be redrawn immediately.

Syntax

expression.Refresh

expression A variable that represents a PivotCache object.

Example

This example refreshes the PivotTable cache for the first PivotTable report on the first worksheet in a workbook.

Worksheets(1).PivotTables(1).PivotCache.Refresh

[!includeSupport and feedback]

Содержание

  1. Excel VBA refresh all Pivot Table or Pivot Cache
  2. Excel VBA refresh all Pivot Table or Pivot Cache
  3. Understanding difference between Pivot Table and Pivot Cache
  4. Refresh all Pivot Table or Pivot Cache
  5. 1) Using RefreshAll Method
  6. 2) Loop through individual Pivot Table and use RefreshTable Method
  7. Excel VBA refresh Pivot Table of specific name
  8. Refreshing Pivot Tables with VBA
  9. The VBA Tutorials Blog
  10. Pivot Tables and Pivot Caches
  11. Refreshing Pivot Table
  12. When Pivot Tables Share a Cache
  13. When Tables Do NOT Share a Cache
  14. Getting Names and Iterating
  15. Refreshing a PivotCache
  16. Refreshing a single PivotCache
  17. Refreshing all PivotCaches
  18. Refreshing on Changes
  19. Excel VBA
  20. Refresh Excel Pivot Table and Cache of PivotTable, using VBA

Excel VBA refresh all Pivot Table or Pivot Cache

This tutorial explains how to use Excel VBA to refresh all Pivot Table or Pivot Cache, and difference between PivotCache.Refresh and PivotTable.RefreshTable

You may also want to read:

Excel VBA refresh all Pivot Table or Pivot Cache

Understanding difference between Pivot Table and Pivot Cache

To start with this topic, you should note the relationship and difference between refresh Pivot Table and Pivot Cache.

When you create a Pivot Table, Pivot Cache is created to store the source data in cache memory to speed up Pivot Table. Afterwards, Pivot Table will use the Pivot Cache as data source. If you are using multiple Pivot Tables using the same data source, all Pivot Tables use the same Pivot Cache in order to minimize your file size.

When you refresh a Pivot Table (right click on the Pivot Table > Refresh), you are refreshing Pivot Cache. Because Pivot Cache is refreshed, Pivot Table also gets refreshed. Therefore, you refresh all Pivot Tables that uses the same Pivot Cache by refreshing just one Pivot Table.

If you want Pivot Table to be independent, you can use Name Range to name the data source to other names (e.g Table1, Table2), and assign different name range to different Pivot Tables.

Read the below three VBA Methods regarding Refresh Pivot Table.

PivotTable.RefreshTable Method – Refreshes the PivotTable report from the source data. Returns True if it’s successful.

PivotTable.PivotCache().Refresh() Method – Updates the cache of the PivotTable object

Although VBA provides two different Methods, all of them will refresh Pivot Cache in the end, so it doesn’t matter which one to use. Therefore in the following sections, I will only show how to refresh Pivot Table.

Refresh all Pivot Table or Pivot Cache

To refresh all Pivot Table, there are two ways.

1) Using RefreshAll Method

The below action of VBA code is same as clicking Data > Refresh All

This method is very simple and refreshes all Pivot Tables in the workbook, however, it also refreshes all other external connections.

2) Loop through individual Pivot Table and use RefreshTable Method

RefreshTable Method is same as right click on a Pivot Table and click “Refresh”. Although the below code looks like only refresh only one Pivot Table for each loop, but as I noted previously, refreshing one Pivot Table would refresh all other Pivot Tables using the same Pivot Cache.

Excel VBA refresh Pivot Table of specific name

The below code shows how to refresh Pivot Table of specific name (as noted above, this will also refresh Pivot Tables using the same Pivot Cache).

You can also loop through each name in the workbook.

Источник

Refreshing Pivot Tables with VBA

The VBA Tutorials Blog

Pivot Tables are powerful tools, but they’re only as strong as the data in them. If the data is outdated, then they are no longer useful for understanding the current state of things and they’re certainly not useful for forward projections. Updating your Pivot Table data when you open your Excel file is very simple through the Excel GUI, but you can easily overlook the need to update regularly. In that case, automated updates might be a better idea. We’ve published a tutorial on scheduling macros before, so once you know how to update Pivot Tables programmatically using VBA, you can always automate the entire process, too.

Pivot Tables and Pivot Caches

There are three main components to a Pivot Table: the original data, the Pivot Cache, and the table itself. A PivotCache is an object that lives at the workbook level, so it can be accessed by any Pivot Table on any worksheet. A PivotTable is a sheet-level object, as it must exist on a particular sheet (otherwise you wouldn’t be able to see it!).

Pivot Caches prepare the original data to be used in a Pivot Table, and one cache can be used for many different tables. Importantly for this tutorial, if two Pivot Tables share the same cache, any refresh of one table will first update the underlying cache and then update all tables associated with that cache. This means updates can very efficiently flow through a workbook, but that is not always desirable. In order to update tables individually via VBA, it is necessary that the tables to be refreshed independently rely on separate caches. You can see which cache any table relies on through its .CacheIndex property.

Refreshing Pivot Table

The easiest way to refresh a table is simply calling the .RefreshTable method on it. If you know the name and location of the table, it is a simple one-liner. From our previous VBA pivot table tutorial, let’s say the table we want to refresh lives on Pivot Sheet1 and the name of the table is AQI for CBSAs 2019 . Our single line of code would be:

Make powerful macros with our free VBA Developer Kit

Tutorials like this can be complicated. That’s why we created our free VBA Developer Kit and our Big Book of Excel VBA Macros to supplement this tutorial. Grab them below and you’ll be writing powerful macros in no time.

Importantly, if any other tables share the same Pivot Cache — that is, their .CacheIndex values are equal — then all associated tables will also update. This is a very efficient implementation because you only need to know the name of one table, and you only need to write one line of code. The underlying programming logic and processing is already optimized by Microsoft and the VBA language developers for us.

So, if a workbook is full of Pivot Tables all built off a single cache, then this approach will actually update the entire workbook without any other coding required.

Sometimes it is important to separate the underlying caches in order to accomplish your needs. One drawback of using one cache for all tables is it becomes impossible to separate tables on certain dimensions, such as groupings. In such cases, it is better to separate caches. If tables do not share a cache, then they can be refreshed in the same way as above, but it will need to be done over the entire collection. This can be accomplished via nested loops, which is shown in the next section.

Getting Names and Iterating

If you know the name of the sheet but not the name of the table, you can always check the .Name property of all the Pivot Tables on that page, which will live in the PivotTables collection. To clarify, PivotTable refers to a single table, while the plural PivotTables actually refers to the collection of tables.

Using debug.print and the Immediate window (keyboard shortcut is Ctrl+g), you can check all the names:

You don’t have to stop here, though. You could nest for-loops and do this for every worksheet and every table (tables on different sheets can have the same name, though!):

and now you have the name of every table and the sheets on which they reside, so you can easily target specific Pivot Tables to be updated.

If you have separate Pivot Caches for each table, you can use this nested for-loop method to update every table. Simply replace the debug.print line above with tbl.RefreshTable , like this:

Using this method, each table in the entire workbook can be updated independently, regardless of its dependence on the same Pivot Cache.

There’s another way to refresh all the pivot tables in a workbook. Instead of iterating through each worksheet and pivot table, you can simply call the .RefreshAll method of the workbook. Here’s how your simplified macro might look if you wanted to refresh all the pivot tables in your workbook:

Refreshing a PivotCache

Earlier we talked about the relationship between Pivot Tables and Pivot Caches. Instead of refreshing a pivot table, you could choose to refresh the pivot cache directly and all pivot tables linked to this cache will automatically be updated. Let’s show you how that looks.

Refreshing a single PivotCache

What if you wanted to update a specific PivotCache rather than a specific pivot table? The code is similar to the first code snippet we presented in this tutorial, but you have to drill down one more level to get to the PivotCache . Take a look:

Notice how this version of the code uses the .Refresh method instead of the .RefreshTable method. That’s because this method refreshes the entire cached data, whereas the .RefreshTable method refreshes the PivotTable from the source data. It’s a subtle difference, I’ll admit.

Refreshing all PivotCaches

Instead of looping through and refreshing all the pivot tables, you could choose to loop through and refresh all the underlying Pivot Caches. Here’s how you would to that:

The downstream pivot tables are automatically updated when the underlying pivot cache is refreshed, so this macro essentially does the same thing as the refresh_all_pivot_tables macro we presented earlier.

Refreshing on Changes

Another nifty feature of Excel is its worksheet events. Worksheet events are similar to workbook events, but they’re triggered by events on a specific sheet. These events can be used to update Pivot Tables whenever the original data is changed (assuming the data lives in the same workbook as the Pivot Tables).

The original data is often updated by hand to fix errors. In those cases, the original datasheet can be monitored for changes with the Worksheet_Change event. Whenever a change occurs to the original data worksheet, a macro can be triggered to update the Pivot Tables.

In order to access worksheet events, you need to open the worksheet’s dedicated code module:


The Dedicated Worksheet Code Module

Double click the red box to get the dedicated code for that worksheet. For our purposes, use the worksheet that contains the editable original data. Then, change the orange box to Worksheet from General , and finally, choose Change from the events dropdown. This will fill in some code that will trigger when changes are detected on that worksheet.

Now, you can add your Pivot Table refresh code in this private subroutine, and any time a change is committed (someone presses enter for new data), the code here will be triggered.

This is particularly useful to ensure tables are always updated, since any change to the original data will flow over to the tables immediately. This frees users from updating the tables manually; they don’t even have to remember to do so, since it will automatically work in the background.

A caveat: this method is only practical for singular changes! If many changes are being made or the pivot table is on the same sheet as the data, Excel will become unusable as it continually updates tables for every single commit. If several changes are to be made to the original data at once, consider another method, such as a button users can press once they finish making their changes. Alternately, you can disable events in your worksheet_change code by toggling Application.EnableEvents, like this:

Updating the information in a Pivot Table is essential for accurate data analysis, and in Excel, it is very easy to do programmatically. While it can be done through the GUI, it is often better to automate the process using a worksheet event. If you’d rather the data only be updated once the user is ready, you could have a button the user presses after changes have been made to the source data.

I hope you enjoyed this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.

Ready to do more with VBA?
We put together a giant PDF with over 300 pre-built macros and we want you to have it for free. Enter your email address below and we’ll send you a copy along with our VBA Developer Kit, loaded with VBA tips, tricks and shortcuts.

Before we go, I want to let you know we designed a suite of VBA Cheat Sheets to make it easier for you to write better macros. We included over 200 tips and 140 macro examples so they have everything you need to know to become a better VBA programmer.

This article was written by Cory Sarver, a contributing writer for The VBA Tutorials Blog. Visit him on LinkedIn and his personal page.

Источник

Excel VBA

Refresh Excel Pivot Table and Cache of PivotTable, using VBA

User Rating: 4 / 5

Refresh Excel Pivot Table and Cache of PivotTable, using VBA

Refresh Data in a PivotTable report: You have different ways to refresh and update data in a PivotTable report with any changes made in the source data. This section explains how to update or refresh a PivotTable report, with VBA code.

Related Links:

7. Refresh Excel Pivot Table and Cache of PivotTable, using VBA

Refer complete Tutorial on using Pivot Tables in Excel Window (user interface):

Contents:

Refresh the Cache of the Pivot Table using the PivotCache.Refresh Method

Refresh all Pivot Tables in a workbook using the PivotCache method. In this example each PivotTable report (starting from the first) in each worksheet is refreshed. Note that each Pivot Table report has one cache only. See below code:

Sub PivotTableRefresh1()

Dim PvtTbl As PivotTable
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

For Each PvtTbl In ws.PivotTables

End Sub

Refresh all PivotTable Caches in the workbook:

For Each PivotCache In ActiveWorkbook.PivotCaches

Refresh all PivotTable Caches in a worksheet:

For i = 1 To Worksheets(«Sheet1»).PivotTables.Count

Refresh only the second PivotTable Cache in a worksheet:

Refresh all PivotTable Caches in the workbook: Using PivotCaches(index), index being the PivotTable cache number. This returns a single PivotCache from the collection of memory caches in a workbook. Note that each Pivot Table report has one cache only. See below code:

For i = 1 To ActiveWorkbook.PivotCaches.Count

PivotTable.RefreshTable Method refreshes and updates the PivotTable report with all information from the data source

Refresh a specific Pivot Table in a worksheet:

Refresh all Pivot Tables in a worksheet:

Dim PvtTbl As PivotTable

For Each PvtTbl In Worksheets(«Sheet1»).PivotTables

Refresh specific Pivot Tables in a worksheet:

Dim PvtTbl As PivotTable
For Each PvtTbl In Worksheets(«Sheet1»).PivotTables

If MsgBox(«Refresh » & PvtTbl & «?», vbYesNo) = vbYes Then

Refresh all Pivot Tables in a workbook:

Dim PvtTbl As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

For Each PvtTbl In ws.PivotTables

Automatically refresh Pivot Tables in all worksheets on opening a workbook, using the PivotTable.RefreshTable Method

Code to automatically refresh PivotTable reports in all worksheets, when the workbook opens, using the Workbook_Open() event handler:

Private Sub Workbook_Open()

‘Automatically refresh Pivot Tables in all worksheets on opening a workbook. Use the Workbook_Open() event handler to execute code when the workbook opens which will automatically reload and refresh the PivotTable reports from their respective source data.

‘To create a workbook event: use the Visual Basic Editor -> in the Project Explorer, double click on «ThisWorkbook» (under ‘Microsoft Excel Objects’ which is under the name of your workbook) -> in the Code
window, select «Workbook» from the left-side «General» drop-down menu and then select the relevant event from the right-side «Declarations» drop-down menu. Workbook events code must be placed in the code
module for the ThisWorkbook object. If you select «Open» here, the code window will show:
‘Private Sub Workbook_Open()
‘End Sub

Dim PvtTbl As PivotTable
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

For Each PvtTbl In ws.PivotTables

End Sub

Return the Date when PivotTable was last refreshed, and Name of the user who refreshed

Источник

Refresh Excel Pivot Table and Cache of PivotTable, using VBA

Refresh Data in a PivotTable report: You have different ways to refresh and update data in a PivotTable report with any changes made in the source data. This section explains how to update or refresh a PivotTable report, with VBA code.


Related Links:

1. Create an Excel Pivot Table report using VBA

2. Referencing an Excel Pivot Table Range using VBA

3. Reference Pivot Fields and Pivot Items in an Excel Pivot Table, using VBA

4. Excel Pivot Table Address, Location, Move & Copy using VBA

5. Excel Pivot Table Layout and Design, using VBA

6. Excel Pivot Table Properties & Settings, using VBA

7. Refresh Excel Pivot Table and Cache of PivotTable, using VBA

8. Excel Pivot Tables Grouping: Group Items, Group Data and Group Date Values, using VBA

9. Excel Pivot Tables: Sort Fields, Values & Dates, use Custom Lists, with VBA

10. Excel Pivot Tables: Filter Data, Items, Values & Dates using VBA

11. Excel Pivot Tables: Summary Functions, Custom Calculations & Value Field Settings, using VBA

12. Excel Pivot Tables: Insert Calculated Fields & Calculated Items, Create Formulas using VBA

Create and Customize a Pivot Table report


—————————————————————————————————————

Contents:

Refresh the Cache of the Pivot Table using the PivotCache.Refresh Method

PivotTable.RefreshTable Method refreshes and updates the PivotTable report with all information from the data source

Automatically refresh Pivot Tables in all worksheets on opening a workbook, using the PivotTable.RefreshTable Method

Return the Date when PivotTable was last refreshed, and Name of the user who refreshed

—————————————————————————————————————

Refresh the Cache of the Pivot Table using the PivotCache.Refresh Method

Refresh all Pivot Tables in a workbook using the PivotCache method. In this example each PivotTable report (starting from the first) in each worksheet is refreshed. Note that each Pivot Table report has one cache only. See below code:

Sub PivotTableRefresh1()

Dim PvtTbl As PivotTable
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

n = 0

For Each PvtTbl In ws.PivotTables

n = n + 1

Next

If n >= 1 Then

For i = 1 To n

ws.PivotTables(i).PivotCache.Refresh

Next i

End If

Next

End Sub

Refresh all PivotTable Caches in the workbook:

For Each PivotCache In ActiveWorkbook.PivotCaches

PivotCache.Refresh

Next

Refresh all PivotTable Caches in a worksheet:

For i = 1 To Worksheets(«Sheet1»).PivotTables.Count

Worksheets(«Sheet1»).PivotTables(i).PivotCache.Refresh

Next i

Refresh only the second PivotTable Cache in a worksheet:

Worksheets(«Sheet1»).PivotTables(2).PivotCache.Refresh

Refresh all PivotTable Caches in the workbook: Using PivotCaches(index), index being the PivotTable cache number. This returns a single PivotCache from the collection of memory caches in a workbook. Note that each Pivot Table report has one cache only. See below code:

For i = 1 To ActiveWorkbook.PivotCaches.Count

ActiveWorkbook.PivotCaches(i).Refresh

Next i

PivotTable.RefreshTable Method refreshes and updates the PivotTable report with all information from the data source

Refresh a specific Pivot Table in a worksheet:

Worksheets(«Sheet1»).PivotTables(«PivotTable1»).RefreshTable

Refresh all Pivot Tables in a worksheet:

Dim PvtTbl As PivotTable

For Each PvtTbl In Worksheets(«Sheet1»).PivotTables

PvtTbl.RefreshTable

Next

Refresh specific Pivot Tables in a worksheet:

Dim PvtTbl As PivotTable
For Each PvtTbl In Worksheets(«Sheet1»).PivotTables

If MsgBox(«Refresh » & PvtTbl & «?», vbYesNo) = vbYes Then

PvtTbl.RefreshTable

End If

Next

Refresh all Pivot Tables in a workbook:

Dim PvtTbl As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

For Each PvtTbl In ws.PivotTables

PvtTbl.RefreshTable

Next

Next

Automatically refresh Pivot Tables in all worksheets on opening a workbook, using the PivotTable.RefreshTable Method

Code to automatically refresh PivotTable reports in all worksheets, when the workbook opens, using the Workbook_Open() event handler:

Private Sub Workbook_Open()

‘Automatically refresh Pivot Tables in all worksheets on opening a workbook. Use the Workbook_Open() event handler to execute code when the workbook opens which will automatically reload and refresh the PivotTable reports from their respective source data.

‘To create a workbook event: use the Visual Basic Editor -> in the Project Explorer, double click on «ThisWorkbook» (under ‘Microsoft Excel Objects’ which is under the name of your workbook) -> in the Code
window, select «Workbook» from the left-side «General» drop-down menu and then select the relevant event from the right-side «Declarations» drop-down menu. Workbook events code must be placed in the code
module for the ThisWorkbook object. If you select «Open» here, the code window will show:
‘Private Sub Workbook_Open()
‘End Sub

Dim PvtTbl As PivotTable
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

For Each PvtTbl In ws.PivotTables

PvtTbl.RefreshTable

Next

Next

End Sub

Return the Date when  PivotTable was last refreshed, and Name of the user who refreshed

Return the Date when PivotTable was last refreshed, using the PivotTable.RefreshDate Property:

MsgBox Worksheets(«Sheet1»).PivotTables(«PivotTable1»).RefreshDate

Return the name of the user who last refreshed the PivotTable, using the PivotTable.RefreshName Property:

MsgBox Worksheets(«Sheet1»).PivotTables(«PivotTable1»).RefreshName

Return to VBA Code Examples

This tutorial will demonstrate how to Refresh a Pivot Table or all Pivot tables using VBA.

Pivot tables are an exceptionally powerful data tool of Excel. Pivot tables enable us to analyze and interpret large amounts of data by grouping and summarizing fields and rows.

When we create a Pivot table, the data is stored in what is known as a Pivot table cache.

If the data source of the Pivot table is updated, the Pivot table itself does not get updated. The user in Excel needs to click on the Refresh All button in the Data tab on the Ribbon to refresh the source data of the Pivot Table.

VBA Pivot RefreshAll

Alternatively, we  can write macros in VBA which will update the data for us!

Refresh all Data Connections

The most simple method to refresh the data is to use VBA to mimic what the Refresh All button does on the Ribbon.

Sub RefreshConnections()
    ActiveWorkbook.RefreshAll
End Sub

This method will refresh all the connections to any data in the workbook that you have active.  It will also not just refresh a single Pivot table but will refresh multiple Pivot tables if you have more than one connected to different data sets.

Refresh all Pivot Tables

To refresh just the Pivot tables in our workbook but exclude any other data connections, we can use a method called RefreshTable.

If we have multiple Pivot tables in our workbook, we would need to loop through all of the Pivot tables in order to refresh all of them.  In order to do this, we first declare a Pivot Table Variable and then create a For Each Loop to loop through all the Pivot tables in the Active workbook.

Sub RefreshPivotsOnly()
  Dim tblPivot As PivotTable
  For Each tblPivot In ActiveWorkbook.PivotTables
     tblPivot.RefreshTable
  Next tblPivot
End Sub

We can use a similar macro to refresh the Pivot tables in our Active Sheet rather than the entire workbook. We would then loop through the Pivot tables in the ActiveSheet rather than the ActiveWorkbook.

Sub RefreshActiveSheetPivotsOnly()
   Dim tblPivot As PivotTable
   For Each tblPivot In ActiveSheet.PivotTables
      tblPivot.RefreshTable
   Next tblPivot
End Sub

This macro would be most useful if we had easy access to it on our ActiveSheet. In order to to this, we can create a button on the sheet to run the macro.

VBA Pivot Button

Refresh one Pivot Table

If we just want to refresh the Pivot table we are working on and not all the other Pivot tables in the workbook, we need to identify the specific Pivot table. This is of course as long as you know the name of the Pivot table – in this case PivotTable1.

Sub RefreshOneTable
   ActiveSheet.PivotTables("PivotTable1").RefreshTable
End Sub

Refreshing the Pivot Table Cache

If we have multiple Pivot tables in our workbook, but they all use the same data, we can refresh the Pivot table cache rather than refreshing the actual Pivot table. Refreshing the cache will then automatically refresh all the Pivot tables that are using the data contained in the cache.

Sub RefreshCache()
   Dim chPivot As PivotCache
   For Each chPivot In ActiveWorkbook.PivotCaches
       chPivot.Refresh
   Next chPivot
End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
vba save as

Learn More!

Понравилась статья? Поделить с друзьями:
  • Excel vba picture load
  • Excel vba listbox снять выделение
  • Excel vba for print
  • Excel vba personal xlsb
  • Excel vba listbox значение