I am having a bizarre issue. What I am doing is copying a worksheet containing pivot tables from one workbook to another. Then I am changing the source data to link to the new workbook, to the same range as the original source but in the new book. My issue is that the original source data is returning a different source to what I see when I go "change data source" manually.
This almost works but the "oldsource" returned is not my actual data source - its the right worksheet but the range "c5:c10" instead of "$E:$J" which is what the pivot source is when I look at it.
Any suggestions would be much appreciated!!
Code:
Sub testpivot()
Dim x
Dim OldSource, newSource As String
Dim PT As PivotTable
For Each PT In ThisWorkbook.Sheets("Test Sheet").PivotTables
OldSource = PT.SourceData
OldSource = VBA.Mid(OldSource, InStr(1, OldSource, "["), 100)
newSource = Replace(OldSource, "Original Book.xlsm", "")
newSource = Replace(newSource, "[]", "")
PT.ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=newSource)
Next PT
End Sub
This almost works but the "oldsource" returned is not my actual data source - its the right worksheet but the range "c5:c10" instead of "$E:$J" which is what the pivot source is when I look at it.
Any suggestions would be much appreciated!!