Change data source in multiple pivot tables

rosiejameson

New Member
Joined
Jan 21, 2015
Messages
1
I have a file containing approximatly 150 pivot tables, all sourced to another work book.

Is there a way in which I can change the data source for all of these pivot tables in one go? The data source is a different workbook that the one the pivot tables are in.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit
[/COLOR]
[COLOR=darkblue]Sub[/COLOR] ChangeDataSourceForAllPivotTables()

    [COLOR=darkblue]Dim[/COLOR] wb                  [COLOR=darkblue]As[/COLOR] Workbook
    [COLOR=darkblue]Dim[/COLOR] ws                  [COLOR=darkblue]As[/COLOR] Worksheet
    [COLOR=darkblue]Dim[/COLOR] pt                  [COLOR=darkblue]As[/COLOR] PivotTable
    [COLOR=darkblue]Dim[/COLOR] sSourceData         [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] ErrHandler
    
    sSourceData = "'C:\Users\Domenic\Desktop\[Book2.xlsm]Sheet1'!A1:C500" [COLOR=green]'change accordingly
[/COLOR]
    [COLOR=darkblue]Set[/COLOR] wb = ActiveWorkbook
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] ws [COLOR=darkblue]In[/COLOR] wb.Worksheets
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] pt [COLOR=darkblue]In[/COLOR] ws.PivotTables
            pt.ChangePivotCache wb.PivotCaches.Create(xlDatabase, sSourceData)
            pt.RefreshTable
        [COLOR=darkblue]Next[/COLOR] pt
    [COLOR=darkblue]Next[/COLOR] ws
    
ExitTheSub:
    
    [COLOR=darkblue]Set[/COLOR] wb = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] ws = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] pt = [COLOR=darkblue]Nothing[/COLOR]
    
    [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
ErrHandler:
    MsgBox "Error " & Err.Number & ":  " & Err.Description, vbCritical, "Error"
    [COLOR=darkblue]Resume[/COLOR] ExitTheSub
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,643
Messages
6,125,990
Members
449,278
Latest member
MOMOBI

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