Changing many pivot tables' sourcedata

j01233210

New Member
Joined
Jun 9, 2011
Messages
3
Hi,

I have a workbook with many sheets and each sheet has several pivot tables. However, this workbook is copied from different workbooks and the pivot tables are referencing to the original workbooks. I want them all now to use the same Table1 as source data in the new workbook. So I wrote this:

HTML:
Sub Macro1()
Dim ws As Worksheet
Dim abc As Integer
For Each ws In ThisWorkbook.Worksheets
    For i = 1 To 20
    abc = i
    ActiveSheet.PivotTables("PivotTable" & abc).ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Table1", Version _
        :=xlPivotTableVersion12)
    Next i
Next
End Sub

The problem is my pivot tables are not ordered in 1,2,3,4,.....20. So it got errors. Can anyone help me out? Huge thx!!!!:confused:
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about this?

Code:
Sub ChangePivotSource()
    Dim pt As PivotTable
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        For Each pt In ws.PivotTables
            pt.SourceData = "Table1"
        Next pt
    Next ws
    
End Sub

Let us know if it works.

Gary
 
Upvote 0
If memory is of concern, you could change the method to point all the PivotTables to the same source.

If all the PivotTables do indeed have the same structure, this code should do the trick.

Code:
Sub ChangeCacheSource()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pt1 As PivotTable
    Dim i As Long
    
    For Each ws In Worksheets
        For Each pt In ws.PivotTables
            If i = 0 Then
                i = 1
                pt.SourceData = "Table1"
                Set pt1 = pt
            Else
                pt.CacheIndex = pt1.CacheIndex
            End If
        Next pt
    Next ws
End Sub
 
Upvote 0
Failed.. I think you cannot directly call
For Each pt In ws.PivotTables
I use debug mode and when it goes to this row, it popped an error.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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