Updating Multiple Pivot Tables

rachelh

New Member
Joined
Aug 15, 2016
Messages
1
Hi All,

I'm trying to create a macro to update multiple pivot tables in one worksheet with an expanded source data location. For example, I have added more data for a new quarter to the range that the pivot tables are reading from and I wanted to automate the process of updated the data source for all the pivot tables.

Any suggestions on how to do this?

I tried doing this (see below) but I was getting an error that "ChangePivotCache" is a "Sub or Function not defined":

Dim Sheet As Worksheet, Pivot As PivotTable

For Each Sheet In ThisWorkbook.Worksheets
For Each Pivot In Sheet.PivotTables
ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"....file path...." R1C1:R5164C42" _
, Version:=xlPivotTableVersion15)
Next
Next
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You'll need to qualify your reference...

Code:
[COLOR=#ff0000]Pivot.[/COLOR][FONT=tahoma]ChangePivotCache _[/FONT]

Also, each time you change the source data for a pivot table, a new pivot cache is created. Therefore, to keep only one pivot cache for all pivot tables, try...

Code:
Dim Sheet As Worksheet, Pivot As PivotTable
Dim iCacheIndex As Integer
Dim bCreated As Boolean

For Each Sheet In ThisWorkbook.Worksheets
    For Each Pivot In Sheet.PivotTables
        If Not bCreated Then
            Pivot.ChangePivotCache ActiveWorkbook.PivotCaches.Create...
            iCacheIndex = Pivot.CacheIndex
            bCreated = True
        Else
            Pivot.CacheIndex = iCacheIndex
        End If
    Next
Next

However, it would be much easier to convert your data into a Table or create a dynamic named range for the data, and use the table name or defined name as the source data for each pivot table . This way, the reference to the table or range will automatically adjust as data is added/removed.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,307
Messages
6,135,750
Members
449,963
Latest member
palm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top