Hi, I've a code which runs to refresh all pivots I've listed in an excel file with a specific data source, also listed in the cells of the excel file.
The code is able to execute, and refreshes the data, however, the data source range becomes corrupt after? It throws me error: "The PivotTable report was saved without the underlying data. Use the Refresh Data command to update the report." and excel would need to restart after this error is being thrown.
I'm assuming that the data source is the one which is corrupted and not the pivot table itself because of below incidents:
1. Refreshing the same pivot table against other data sources would not throw the same error.
2. Using original data source listed in my excel file to create a new pivot would throw the error.
Is there something I need to change in my code? Below is the code:
Thanks in advance!
The code is able to execute, and refreshes the data, however, the data source range becomes corrupt after? It throws me error: "The PivotTable report was saved without the underlying data. Use the Refresh Data command to update the report." and excel would need to restart after this error is being thrown.
I'm assuming that the data source is the one which is corrupted and not the pivot table itself because of below incidents:
1. Refreshing the same pivot table against other data sources would not throw the same error.
2. Using original data source listed in my excel file to create a new pivot would throw the error.
Is there something I need to change in my code? Below is the code:
Code:
Option Explicit
Public Data_Source_1400 As Range
Public Data_Source_1001 As Range
Public Data_Source As Range
Public Pivot_Workbook As String
Public Pivot_Sheet As String
Public Pivot_Table_Name As String
Public Ref_Sheet As Worksheet
Sub Refresh_Pivots()
Set Ref_Sheet = Workbooks("MACRO BUTTONS.xlsm").Worksheets("Refresh Pivots")
Set Data_Source_1400 = Ref_Sheet.Range("RDD1400_Range")
Set Data_Source_1001 = Ref_Sheet.Range("RDD1001_Range")
'Loops through first column in sheet.
With Ref_Sheet
Dim i As Integer
For i = 2 To 20
If .Cells(i, 1).Value <> "" Then
Pivot_Workbook = .Cells(i, 1).Value
Pivot_Sheet = .Cells(i, 2).Value
Pivot_Table_Name = .Cells(i, 3).Value
'Chooses different range based on whether it's for 1400_N or 1001
If .Cells(i, 4).Value = "1400_N" Then
Set Data_Source = Data_Source_1400
Else
Set Data_Source = Data_Source_1001
End If
Call Refresh_Pivot(Pivot_Workbook, Pivot_Sheet, Pivot_Table_Name, Data_Source)
End If
Next
End With
End Sub
Function Refresh_Pivot(Pivot_Workbook As String, Pivot_Sheet As String, Pivot_Table_Name As String, Data_Source As Range)
'Change data source of pivot.
Workbooks(Pivot_Workbook).Worksheets(Pivot_Sheet).PivotTables(Pivot_Table_Name).ChangePivotCache _
Workbooks(Pivot_Workbook).PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Data_Source)
'Ensure pivot table is refreshed.
Workbooks(Pivot_Workbook).Worksheets(Pivot_Sheet).PivotTables(Pivot_Table_Name).RefreshTable
End Function
Thanks in advance!