How to write a macro to change data source of a pivot.

samrat

New Member
Joined
Aug 5, 2011
Messages
10
Hi,

Can anybody let me know how to write a macro for changing the data source of a pivot?

I need to prepare a file every week by pasting a extract, formating it and then updating the pivot based out of this extract in the same file.

I have automatised the pasting and formating part but not able to change the data source of the pivot, which is essential as the amount of data varies every week.

Thanks
Sam
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

This is how I've done it. Hope it can be useful to you.


Sub Update_Pivot_Infra_Source()
Application.ScreenUpdating = False

' Data Infra is the sheet where I have the pivot source
' Cost & Price Total is the sheet where I have the Pivot table

Dim pt As PivotTable

xRowsI = Sheets("Data Infra").Cells(Cells.Rows.Count, 1).End(xlUp).Row
xRangeI = "Data Infra!R66C1:R" & xRowsI & "C24"

Sheets("Data Infra").Select
Sheets("Cost & Price Total").Select For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
pt.SourceData = xRangeI
Next pt

End Sub

//RTN
 
Upvote 0
Thanks

I will definitely try this, and get back to you for any further suggestion.

Thanks Again
Sam
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,384
Members
452,908
Latest member
MTDelphis

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