I am now using this code, it never generates any errors and seem to work fine:
Sub Refresh_All_Pivot_Tables()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim PT As PivotTable
Dim Source As String
Source = Worksheets("MasterSheet").Range("A1:D1").CurrentRegion.Address(True, True, xlR1C1, True)
For Each ws In ActiveWorkbook.Worksheets
For Each PT In ws.PivotTables
PT.SourceData = Source
Next PT
Next ws
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
I have one outstanding issue though, I want the new range to be from columns "A1:D1" until last row, but the above code select the whole table, i.e. from "A1:J1) until the last row.
I used this code as an alternative but this did not help:
Dim ws As Worksheet
Dim PT As PivotTable
Dim LastRow As Long
Dim Source As String
LastRow = Worksheets("MasterSheet").Range("A" & Rows.Count).End(xlUp).Row
Source = Worksheets("MasterSheet").Range("A1:D1" & LastRow).CurrentRegion.Address(True, True, xlR1C1, True)
For Each ws In ActiveWorkbook.Worksheets
For Each PT In ws.PivotTables
PT.SourceData = Source
Next PT
Next ws
Any idea and making sure the range is only for specific columns rather than all of them?
Bookmarks