VBA to change data source of pivot tables

Lefemmenikita

Board Regular
Joined
Jan 28, 2014
Messages
59
Office Version
  1. 2013
Platform
  1. Windows
Hi

I have multiple excel workbooks with pivot tables pointing to an external spreadsheet (referred to as 'source')


Every month, I save both the excel workbooks and the source workbook into a new folder.



I don't rename any of the files.

is there any vba code which I can use to update the data source of the pivot tables in the target file so it points to the 'source' workbook which is saved in the same folder as it?



e.g. In the Sep-16 subfolder, if there is a workbook with a pivot, I want the pivot to point to the workbook called 'source' which is also saved in the Sep sub folder and not the file called 'source' saved in the Aug-16 subfolder.

Hope this makes sense.

I have attached screencaps of what this is supposed to look like

Capture.png

Capture1.png



Capture3.png
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Nikita
You could use something like this
Code:
Sub setRefToCurrentPath()
    Dim myPath As String
    myPath = Application.ActiveWorkbook.Path
    ActiveSheet.PivotTables("YrPivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        myPath & "\[source.xlsx]Sheet1!R1C1:R19C4", Version:=6)
    ActiveSheet.PivotTables("YrPivotTable1").PivotCache.Refresh
End Sub

Where
YrPivotTable1 is the name of your pivot table
Sheet1!R1C1:R19C4 is the range of your data in source.xlsx workbook

Do not use these names use your names in the macro

Cheers
Sergio
 
Upvote 0
Hi Sergio

Thanks for your help.


I tried this now and updated the sheet name references and the pivot table name ('pivottable1' to reflect the name per the actual table) and am getting the error:


'Run-time error 5:

Invalid procedure call or argument'


Thanks
 
Upvote 0
It looks you may have committed an error when replacing the values of pivot table name, sheet or range
Because my code was working in my test sheet with no error 5
Cheers
Sergio
 
Upvote 0
This is what I am using:


Code:
Sub setRefToCurrentPath()
    Dim myPath As String
    myPath = Application.ActiveWorkbook.Path
    ActiveSheet.PivotTables("[B]PivotTable1[/B]").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        myPath & "\[source.xlsx][B]Sheet1[/B]!R1C1:R19C4", Version:=6)
    ActiveSheet.PivotTables("[B]PivotTable1[/B]").PivotCache.Refresh
End Sub


I have checked the name of the pivot and the name of the sheet.

I'm not sure where I am going wrong?
thanks
 
Upvote 0
I think the error 5 is because you did not change the data range in source file, your macro uses the same range I used
"\[source.xlsx]Sheet1!R1C1:R19C4" you have to change to reflect the sheet name and range in RC format
Sergio
 
Upvote 0
The file was called 'source.xlsx' and the sheet was 'sheet1'

The row and column references (19 rows, 4 columns) also fit so it's most likely not that I think
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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