macro to change data source for pivot table

JoeSarton

New Member
Joined
Aug 12, 2011
Messages
3
Hello -

Hoping someone can help me with my question regarding source data and pivot tables. One a tab I have columns of data which is the source data for a pivot table I have just below. For the sake of this question, lets say all of this is on "Sheet1", the data is in cells A2:F25, and the pivot table is in cell G27.

The problem comes when I make a copy of the tab (keeping the copied tab in the same workbook), change the data in this new tab (called Sheet2), the pivot table still uses the data from Sheet1 (it seems to automatically anchor the data source to that original tab). Is there a way that I can run a macro so that it will change the data source for the pivot table in Sheet2 to run off of the data in Sheet2.

The reason I don't want to manually adjust the source data is that I will have many tabs and hoping there was a macro that I could run on each tab to change the data source. Hopefully this question is clear enough that someone can help.

I don't know if it makes a difference, but I am in Excel 2010

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Welcome to the Board!


Code:
Sub Update_PTSource()
    With ActiveSheet
        .PivotTables(1).ChangePivotCache ActiveWorkbook. _
            PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:="'" & .Name & "'!PTsource")
    End With
End Sub


Hi all!

After execution of this code a slicer connections to different PTs are lost (there were 3 ones). Could you give me an advise how to get those back? Form Connections report contain only current PT.
Thanks!
 
Upvote 0
Hi vallemedef,

This thread dealt with the topic of disconnecting and reconnecting slicers when a PivotTable source data is changed.

https://www.mrexcel.com/forum/excel...ect-slicers-active-worksheet.html#post3637902

It's a long thread, but the key point is that it's much better to setup your PivotTable's initially with a dynamic reference to a source (like an Excel Table or Dynamic Named Range), than to use VBA to disconnect and reconnect slicers each time the source needs to change.

If you already have PivotTable(s) setup with static (fixed range) addresses, you can adapt the code shown in Post #19 of that thread to convert those over to a dynamic data source.
 
Upvote 0

Forum statistics

Threads
1,223,628
Messages
6,173,429
Members
452,514
Latest member
cjkelly15

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