VBA Copy a PivotTable from another file and change the data source

Palucci

Banned user
Joined
Sep 15, 2021
Messages
138
Office Version
  1. 365
Platform
  1. Windows
I have a macro part that would copy a sheet with a pivot table from another file to another and change the data range to the one already indicated in the target file. However, when I try, I get two errors once "Subscript out of range" or "Invalid Procedure Call Or Argument Error"
wbStart is a file where there is only a button to start a macro and one tab to be move,
My code:
VBA Code:
Set wb = Workbooks.Open(MyFolder & "\" & MyFile, UpdateLinks:=0)
Set wbStart = ThisWorkbook
wbStart.Sheets("Tabela_COREP").Copy After:=wbMe.Sheets(1)
wb.Sheets("Tabela_COREP").PivotTables("Tabela przestawna1").ChangePivotCache wb.Sheets("Tabela_COREP"). _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:="corep", Version:=6)
'Usuwamy alerty o akutliazacji itp'
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Solution :
VBA Code:
Set PivTbl = wbMe.Sheets("Tabela_COREP").PivotTables("Tabela przestawna1")
PivTbl.ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:="corep", _
        Version:=6)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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