Hi guys,
I have some code that does its job in adding datafields to a pivot table. I linked it with a form control button:
So the above works when I click on the button once and it adds the fields to my pivot table. But if I click it again, it gives me a run-time error, '1004' which is expected because its trying to add a data field that already exists.
Is there a line of code that can help me remove this error? Ideally I want this button to be pressed once and it will populate the pivot table, but if it is pressed again many times no errors wil lshow and the data fields will remain
I have some code that does its job in adding datafields to a pivot table. I linked it with a form control button:
Code:
Sub PDA1P()
'
' PDA1P Macro
' Adds all 1P PDA columns into values of PT
'
' Keyboard Shortcut: Ctrl+Shift+P
'
ActiveSheet.PivotTables("SpendTable").AddDataField ActiveSheet.PivotTables( _
"SpendTable").PivotFields("1P PDA Spend"), "1P PDA Spend ", xlSum
ActiveSheet.PivotTables("SpendTable").AddDataField ActiveSheet.PivotTables( _
"SpendTable").PivotFields("1P PDA Sales"), "1P PDA Sales ", xlSum
ActiveSheet.PivotTables("SpendTable").AddDataField ActiveSheet.PivotTables( _
"SpendTable").PivotFields("1P PDA ACOS"), "1P PDA ACOS ", xlSum
With ActiveSheet.PivotTables("SpendTable").PivotFields("1P PDA Spend ")
.NumberFormat = "$#,##0.00"
End With
With ActiveSheet.PivotTables("SpendTable").PivotFields("1P PDA Sales ")
.NumberFormat = "$#,##0.00"
End With
With ActiveSheet.PivotTables("SpendTable").PivotFields("1P PDA ACOS ")
.NumberFormat = "0.00%"
End With
End Sub
So the above works when I click on the button once and it adds the fields to my pivot table. But if I click it again, it gives me a run-time error, '1004' which is expected because its trying to add a data field that already exists.
Is there a line of code that can help me remove this error? Ideally I want this button to be pressed once and it will populate the pivot table, but if it is pressed again many times no errors wil lshow and the data fields will remain
Last edited: