Search code examples
excelolappowerpivotvba

Excel 2013 - VBA AddDataFiled 1004 Error


strange one this

I have a pivot table connected to a data source via power pivot / OLAP. I have created a vba button to add a field to the row section of the pivot however when I run...

With ActiveSheet.PivotTables("PivotTable1")
 .AddDataField ActiveSheet.PivotTables( _
 "PivotTable1").CubeFields("[Measures].[Sum of Unique_Customers]"), "Unique Customers"
End With

I get the object error 1004 runtime error 1004 application-defined or object-defined error.

However if I refresh the pivot / data connection then it works fine. I have put in place a workaround to refresh the pivot each time the workbook opens but I fear as my data grows this will become resource heavy. Does anyone know of a solution or a reason to why the error occurs? It seems vb does not recognise the field until the data is refreshed. For info the field is a raw field and not a MDX calc or anything

Many thanks!


Solution

  • Hi managed to find a solution it was simple in the end - I had "On Error Resume Next" in parts of my code (from an old version) I took these out and its now working :)