Hello -- I have the below VBA code to update three different pivot tables in my Workbook. All of the Pivot Tables use the same data source from the same worksheet. The rows of data change each time I update the source data so I need the pivot table to update automatically when I run the below code. I keep receiving an error.
VBA Code:
Sub Update_Pivot()
'Update source range for Pivot Tables
Dim pt As PivotTable
Dim pc As PivotCache
Dim source_data As Range
lstrow = Cells(Rows.Count, 1).End(xlUp).Row
lstcol = Cells(1, Columns.Count).End(xlToLeft).Column
Set source_data = Range(Cells(1, 1), Cells(lstrow, lstcol))
Set pc = ThisWorkbook.PivotCaches.Create(xlDatabase, SourceData:=source_data)
Set pt = Sheet2.PivotTables("PivotTable1")
Set pt2 = Sheet3.PivotTables("PivotTable2")
Set pt3 = Sheet4.PivotTables("PivotTable3")
pt.ChangePivotCache pc
pt2.ChangePivotCache pc
pt3.ChangePivotCache pc
'Refresh Pivot Tables
ActiveWorkbook.RefreshAll
End Sub