VBA: Error with adding datafields to a pivot table

sinasdf

New Member
Joined
Dec 4, 2017
Messages
37
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:


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:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Welp, solved it myself. For those who are looking for a solution, I put this at the start of code:

ActiveSheet.PivotTables("SpendTable").DataPivotField.Orientation = xlHidden
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top