I created macro which would refresh all pivot tables in a worksheet. The same code works on one sheet however doesn't on the other. I tried to run it in debug mode and it works fine for first few tables and crush on one in the middle with error:
Run-time error '1004': RefreshTable method of PivotTable class failed.
There are 20 Pivot Tables in this sheet, but on the one where it works are 26. Your help would be much appreciated.
Sub Refresh_Pivots2()
Dim PL As PivotTable
Worksheets("Incidents Pivots").Activate
For Each PL In ActiveSheet.PivotTables
PL.RefreshTable
Next PL
End Sub
I just tried another code
Sub Refresh_Incidents()
Worksheets("Incidents Pivots").Activate
Dim A
A = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")
For i = 0 To 19
ActiveSheet.PivotTables(A(i)).RefreshTable
Next i
End Sub
if I start from i=0 it gives an error but when switch it to i=1 it works. Any ideas what is wrong with this table. I am able to refresh it manually.
As Rory suggested there was something wrong with source data for one table, it used A1 instead of R1C1 notation. I changed it in Excel Option->Formulas->R1C1 reference style and now macro works.
'connection property'=>'usage'=>'enable background refresh', please make sure, that it has not been selected.
Have you checked the pivot table datasource. also some time this kind of issues happen due to pivot cache. call the below code before refreshing the pivots.
I have a code that auto adjust the data from a Pivot Table and then refresh the Pivot Table, may be it can halp you: