How to Clear Pivot Table Cache Using VBA in Excel

If we delete some data from the source table of a Pivot Table, in the Pivot Table that data will still be available because the deleted cells are still present in the cache of the Pivot Table. So, we need to clear the cache of the Pivot Table so it matches the modified dataset.

In this article, we will demonstrate how to clear the Pivot Table cache using VBA code in Excel.


Step-by-Step Procedure to Clear Cache from PivotTable Using VBA in Excel

Suppose we have the Sales Data of XYZ Company as our dataset. Let’s clear the Pivot Table cache using VBA code.

excel vba clear pivot cache

 

Step 1 – Insert a Pivot Table

First we’ll create a Pivot Table so that we can clear its cache.

  • Select the entire dataset.
  • Go to the Insert tab from Ribbon.
  • Select the PivotTable option from the Tables group.

Insert Pivot Table to clear the PivotTable cache using VBA code in Excel

The PivotTable from table or range dialog box will open.

  • Choose the Existing Worksheet option.
  • Select the destination cell in the Location field (here, cell F4).
  • Click OK.

The PivotTable Fields dialog box will open.

  • Drag the Employee Name field to the Rows section.
  • Drag the Sales field to the Values section.

Editing PivotTable Fields dialogue box to clear the PivotTable cache using VBA code in Excel

We have the following Pivot Table in our worksheet.

Final output of Step 01 to clear the PivotTable cache using VBA code in Excel

Read More: How to Clear Pivot Cache in Excel 


Step 2 – Delete Data from the Source Table

Now, we will delete data from our source table to create a cache mismatch.

  • Select the data to delete.
  • Go to the Home tab on the Ribbon.
  • Click on the Delete option from the Cells group.
  • Select the Delete Cells option from the drop-down.

Delete Data from Source Table to clear the PivotTable cache using VBA code in Excel

A dialog box named Delete will pop up.

  • Choose the Shift cells up option.
  • Click OK.

Editing Delete dialogue box to clear the PivotTable cache using VBA code in Excel

The selected cells are deleted from the dataset.

Final output of step 02 to clear the PivotTable cache using VBA code in Excel


Step 3 – Insert a Module to Write VBA Code

In order to write the VBA code, we need to create a blank Module.

  • Go to the Developer tab on the Ribbon.
  • Choose the Visual Basic option from the Code group.

Insert Module to Write VBA Code to clear the PivotTable cache using VBA code in Excel

The Microsoft Visual Basic for Applications window will open.

  • Go to the Insert tab.
  • Select the Module option from the drop-down.

Final output of step 03 to clear the PivotTable cache using VBA code in Excel

Read More: How to Clear Excel Cache Using VBA 


Step 4 – Write and Save the VBA Code

  • Enter the following code in the newly created Module1:
Private Sub clear_pivotTable_cache()
Dim table As PivotTable
Dim Worksheet As Worksheet
Dim cache As PivotCache
Application.ScreenUpdating = False
For Each Worksheet In ActiveWorkbook.Worksheets
For Each table In Worksheet.PivotTables
table.PivotCache.MissingItemsLimit = xlMissingItemsNone
Next table
Next Worksheet
For Each cache In ActiveWorkbook.PivotCaches
On Error Resume Next
cache.Refresh
Next cache
Application.ScreenUpdating = True
End Sub

Writing the VBA code Write and Save VBA Code

Code Breakdown

  • We initiated a sub-procedure named clear_pivotTable_cache.
  • We declared three variables named table as PivotTable, Worksheet as Worksheet, and cache as PivotCache.
  • We set the Application.ScreenUpdating property as False.
  • We used two nested For Next loops to remove the excess data from the Pivot Table.
  • We used another For Next loop to refresh the cache.
  • We set the Application.ScreenUpdating property as True.
  • We ended the sub-procedure.
  • Click on the Save option.

Read More: Clear Excel Memory Cache Using VBA 


Step 5 – Run the VBA Code

After saving the code, we will run it to clear the Pivot Table cache.

  • Click on the Run option as marked in the following image.

Note: Alternatively, use the keyboard shortcut F5 to run the code.

Running the VBA code to clear the PivotTable cache using VBA code in Excel

The deleted data is removed from the Pivot Table, confirming that the caches are cleared for the Pivot Table.

Final output of step 5 to clear the PivotTable cache using VBA code in Excel


Download Practice Workbook


 

Get FREE Advanced Excel Exercises with Solutions!
Zahid Hasan
Zahid Hasan

Zahid Hassan, BSc, Industrial and Production Engineering, Bangladesh University of Engineering and Technology, has worked with Exceldmy for 1.5 years. He has written 95+ articles for Exceldemy. He has worked as an Excel & VBA Content Developer. He also worked as a VBA Developer for the Template team. Currently, he is working as a Junior Software Developer for the Excel Add-in project. He is interested in Software Development, Python, VBA, VB.NET, and Data Science, expanding his expertise in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo