VBA to change PT datasource

DRANDON

Active Member
Joined
Jun 30, 2006
Messages
268
I would like to know a relatively simple vba snippet to change an existing pivot tables datasource. Macro recording doesn't reveal anything (just blank with the exception of a cell selection)

I've tried this with errors..

Worksheets("Pivot").PivotTables("Pivot_1").PivotCache.DataSource = _
Range("A2:B10")

I really appreciate any help - thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Re: VBA to chnage PT datasource

For what it's worth, that link did help me :o) I wound up using the one line, created a defined range called "PTDefinedRange", and then I created a sub routine to change the datasource for each of my pivot tables in a multi-tabbed file to my defined range. I'm no wizard, but it seemed to work!

Sub UpdatePTDataSource()
Dim pt As PivotTable
Dim wks As Worksheet

Application.ScreenUpdating = False

For Each wks In ActiveWorkbook.Worksheets
For Each pt In wks.PivotTables
pt.SourceData = "PTDefinedRange"
Next pt
Next wks

Set pt = Nothing
Set wks = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
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