Change source data for pivotcache in vba?

Asator

Board Regular
Joined
Apr 5, 2010
Messages
186
I've got some code that consolodates my pivot tables into one pivot cache, but I can't get my code to change the source data for that pivot cache to my range. How do I go about doing that?

Code:
Public Sub pt_refresh()
Dim pt As PivotTable
Dim ws As Worksheet
Dim pf As PivotFields
Dim pc As PivotCache
Dim p_rng As Range
Dim ws_recon As Worksheet
Set ws_recon = ActiveWorkbook.Sheets("retro price recon")
With ws_recon
    Set p_rng = .Range(.Cells(10, 2), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count))
End With
For Each pc In ActiveWorkbook
    pc.SourceData = p_rng
Next pc
For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
        pt.CacheIndex = 1
        pt.RefreshTable
    Next pt
Next ws

It dies on "for each pc in activeworkbook", but I tried activeworkbook.pivotcaches(1).sourcedata = p_rng and that failed too. Ideas?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

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