VBA to change a Pivot table data source to an external file

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
I have a large workbook with many pivot tables linked to 2 external sources. VBA loops through sheets and pivot tables, then uses this code to change it
VBA Code:
pt.ChangePivotCache wkb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=strROINewSrc)
where pt is the pivot table and
strROINewSrc = 'F:\finance\Report 22\MUEIRE\03 Mar 22\Sales\[EIRESales (Mar - 22).xlsx]!Sales Datasheet'!$A:$AX"
strROINewSrc was created using GetOpenFilename so the file s there and it does have a tab called Sales Datasheet

But I get an error that it can't open the pivot table source.
1652272652549.png


yet I can go and manually change it on the worksheet so the new source file doesn't need to be open.

what am I doing wrong?
 

Attachments

  • 1652272571047.png
    1652272571047.png
    2.3 KB · Views: 13

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It looks like you missing an opening quote, and you have an extra exclamation mark after the workbook name but before the sheet name. So it looks like it should be . . .

VBA Code:
strROINewSrc = "'F:\finance\Report 22\MUEIRE\03 Mar 22\Sales\[EIRESales (Mar - 22).xlsx]Sales Datasheet'!$A:$AX"

Hope this helps!
 
Upvote 0
Solution
Cheers. My old eyes can't see these things any more!
 
Upvote 0
It worked, but now crashes because it runs out of memory. I'll do a new thread.
 
Upvote 0
Sorted. Answer - don't add a lot of data sources with a million rows when you data sources all have less than 10,000
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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