I have written the following code to edit the range of a two pivot tables. Up until the line of stars my code functions properly, however I get a "Run-time error '1004': Application-defined or object-defined" error on the .PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
line. I have no idea what the cause of the error is as I copied the code from above and only changed the sheet name and pivot table name (they both definitely exist in my file). Any help?
Dim RowCount As Integer
Dim ColCount As Integer
Dim rng As Range
Dim CurrentPeriod As String
Dim PivotList As Variant
Dim Piv As String
Dim PivotSht As String
Dim PivotNme As String
RowCount = WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
ColCount = WorksheetFunction.CountA(Sheets("Data").Range("1:1"))
Set rng = Sheets("Data").Range(Sheets("Data").Cells(1, 1), Sheets("Data").Cells(RowCount, ColCount))
CurrentPeriod = Sheets("Static").Range("CurrentPeriod")
With Sheets("Val Cat Current Returns (Adj)").PivotTables("CatCurrentPivot1")
.PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
.PivotFields("Period_id").CurrentPage = CurrentPeriod
.PivotCache.Refresh
End With
**********************************
With Sheets("Val Cat Trend Returns (Adj)").PivotTables("CatTrendPivot1")
.PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
.PivotCache.Refresh
End With