10/15/2016 Excel
Pivot Table Pivot Cache
(http://www.contextures.com/index.html)
Excel Pivot Cache
These Excel macros will help you work with a pivot cache a special memory area where pivot table
records are saved
Machine Learning at Scale
Avoid Those Common Mistakes. Download the Paper to Learn How. Go to
mathworks.com/Machine_Learning
Show a Pivot Table's CacheIndex
Pivot Cache Memory Used
Number of Pivot Caches
Pivot Cache Record Count
Change the Pivot Cache
Create New Pivot Cache for a Pivot Table
Remove Duplicate Pivot Caches
Download the Sample Files
More Tutorials
Show the Pivot Table's CacheIndex
You can display a pivot table's CacheIndex number by using the following User Defined Function. Store
the function code in a worksheet module. Then, on the worksheet, enter the formula:
=ShowCacheIndex(A3)
replacing A3 with a cell in your pivot table.
Function ShowCacheIndex(rngPT As Range) As Long
ShowCacheIndex = rngPT.PivotTable.CacheIndex
End Function
Show the Pivot Cache Memory Used
http://www.contextures.com/xlPivot11.html 1/11
10/15/2016 Excel Pivot Table Pivot Cache
You can display the memory used by a pivot cache, by using the following User Defined Function. Store
the function code in a worksheet module. Then, on the worksheet, enter the formula:
=GetMemory(A3)/1000
replacing A3 with a cell in your pivot table. The result is displayed in kilobytes.
Function GetMemory(rngPT As Range) As Long
'pivot table tutorial by contextures.com
Dim pt As PivotTable
Set pt = rngPT.PivotTable
GetMemory = ActiveWorkbook _
.PivotCaches(pt.CacheIndex).MemoryUsed
End Function
Show the Pivot Cache Count
You can display the number of pivot caches in the active workbook, by using the following macro. Store
the code in a regular code module.
Sub CountCaches()
MsgBox "There are " _
& ActiveWorkbook.PivotCaches.Count _
& " pivot caches in the active workook."
End Sub
Show the Pivot Cache Record Count
You can display the number of records in a pivot cache, by using the following User Defined Function.
Store the function code in a worksheet module. Then, on the worksheet, enter the formula:
=GetRecords(A3)
replacing A3 with a cell in your pivot table.
Function GetRecords(rngPT As Range) As Long
'pivot table tutorial by contextures.com
Dim pt As PivotTable
Set pt = rngPT.PivotTable
GetRecords = ActiveWorkbook _
.PivotCaches(pt.CacheIndex).RecordCount
End Function
Change the Pivot Cache
http://www.contextures.com/xlPivot11.html 2/11
10/15/2016 Excel Pivot Table Pivot Cache
If you have created several Pivot Tables in a workbook, you may find it more efficient to use the same
pivot cache for all the Pivot Tables. The following code will change the pivot cache for each pivot table in
the workbook.
Sub ChangePivotCache()
'pivot table tutorial by contextures.com
'change pivot cache for all Pivot Tables in workbook
Dim pt As PivotTable
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
For Each pt In wks.PivotTables
pt.CacheIndex = Sheets("Pivot").PivotTables(1).CacheIndex
Next pt
Next wks
End Sub
Create New Pivot Cache for Selected Pivot
Table
If two or more pivot tables are based on the same pivot cache, they will share some features, such as
calculated items and grouped fields.
To create a separate pivot cache for a pivot table, you can select a cell in the pivot table, and then run the
following code.
The code adds a sheet to the workbook, and creates a new pivot table there, based on the same data
source, but in a new pivot cache.
The selected pivot table is set to the same pivot cache as the new table, and then the temporary sheet is
deleted.
http://www.contextures.com/xlPivot11.html 3/11
10/15/2016 Excel Pivot Table Pivot Cache
Sub SelPTNewCache()
Dim wsTemp As Worksheet
Dim pt As PivotTable
On Error Resume Next
Set pt = ActiveCell.PivotTable
If pt Is Nothing Then
MsgBox "Active cell is not in a pivot table"
Else
Set wsTemp = Worksheets.Add
ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=pt.SourceData).CreatePivotTable _
TableDestination:=wsTemp.Range("A3"), _
TableName:="PivotTableTemp"
pt.CacheIndex = wsTemp.PivotTables(1).CacheIndex
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End If
exitHandler:
Set pt = Nothing
End Sub
Remove Duplicate Pivot Caches
Multiple pivot tables in a workbook may be based on the same data source, but use different pivot
caches. This macro creates a list of pivot caches, checks for duplicate data sources, and eliminates
duplicate caches.
WARNING: Test this on a copy of your workbook, to ensure that it will work as expected with your
data.
http://www.contextures.com/xlPivot11.html 4/11
10/15/2016 Excel Pivot Table Pivot Cache
Sub CheckCaches()
' Developed by Contextures Inc.
' www.contextures.com
Dim pc As PivotCache
Dim wsList As Worksheet
Dim lRow As Long
Dim lRowPC As Long
Dim pt As PivotTable
Dim ws As Worksheet
Dim lStart As Long
lStart = 2
lRow = lStart
Set wsList = Worksheets.Add
For Each pc In ActiveWorkbook.PivotCaches
wsList.Cells(lRow, 1).Value = pc.Index
wsList.Cells(lRow, 2).Value = pc.SourceData
wsList.Cells(lRow, 3).FormulaR1C1 = _
"=INDEX(R1C[‐2]:R[‐1]C[‐2],MATCH(RC[‐1],R1C[‐1]:R[‐1]C[‐1],0))"
lRow = lRow + 1
Next pc
For lRowPC = lRow ‐ 1 To lStart Step ‐1
With wsList.Cells(lRowPC, 3)
If IsNumeric(.Value) Then
For Each ws In ActiveWorkbook.Worksheets
Debug.Print ws.Name
For Each pt In ws.PivotTables
Debug.Print .Offset(0, ‐2).Value
If pt.CacheIndex = .Offset(0, ‐2).Value Then
pt.CacheIndex = .Value
End If
Next pt
Next ws
End If
End With
Next lRowPC
'uncomment lines below to delete the temp worksheet
'Application.DisplayAlerts = False
'wsList.Delete
exitHandler:
Application.DisplayAlerts = True
Exit Sub
errHandler:
http://www.contextures.com/xlPivot11.html 5/11
10/15/2016 Excel Pivot Table Pivot Cache
MsgBox "Could not change all pivot caches"
Resume exitHandler
End Sub
Download the Sample Files
Download the zipped sample data file (PivotSales.zip) for this pivot table tutorial
Download the sample file with the Remove Duplicate Pivot Cache code (PivotCacheFix.zip).
More Pivot Table Tutorials
FAQs Pivot Tables (../xlfaqPivot.html)
Pivot Table Introduction (../xlPivot01.html)
Grouping Data (../xlPivot07.html)
Multiple Consolidation Ranges (../xlPivot08.html)
Running Totals (../xlPivot14.html)
Summary Functions (../excelpivottablesummaryfunctions.html)
Clear Old Items in Pivot Table (../xlPivot04.html)
Search Contextures Sites Search
More Links
FAQs Pivot Tables (../xlfaqPivot.html)
Pivot Table Introduction (../xlPivot01.html)
Grouping Data (../xlPivot07.html)
Multiple Consolidation Ranges (../xlPivot08.html)
Running Totals (../xlPivot14.html)
Summary Functions (../excelpivottablesummaryfunctions.html)
Clear Old Items in Pivot Table (../xlPivot04.html)
http://www.contextures.com/xlPivot11.html 6/11
10/15/2016 Excel Pivot Table Pivot Cache
Get Excel
News
Name:
Email:
Get Started
(http://www.contextures.com/pivotpowerfreeaddin.html)
http://www.contextures.com/xlPivot11.html 7/11
10/15/2016 Excel Pivot Table Pivot Cache
(xlPivotPremAddIn.html)
http://www.contextures.com/xlPivot11.html 8/11
10/15/2016 Excel Pivot Table Pivot Cache
(http://www.contextures.com/datavalidationmultiselectpremium.html)
http://www.contextures.com/xlPivot11.html 9/11
10/15/2016 Excel Pivot Table Pivot Cache
http://www.contextures.com/xlPivot11.html 10/11
10/15/2016 Excel Pivot Table Pivot Cache
(http://www.contextures.com/exceltoolsaddin.html)
Copyright © Contextures Inc. 2016
Privacy Policy (/privacy.html)
(https://mvp.microsoft.com/enus/mvp/Debra%20%20Dalgleish7612)Debra Dalgleish
(https://mvp.microsoft.com/enus/mvp/Debra%20%20Dalgleish7612)
Last updated: August 7, 2016 4:14 PM
Contextures RSS Feed (http://www.contextures.com/feed.xml)
http://www.contextures.com/xlPivot11.html 11/11