VBA/Macro Pivot Tables - Crashing

robbnobb4

New Member
Joined
Jan 27, 2015
Messages
1
I'm having an issue with this piece of code causing Excel to crash every time it is run. When I remove the bold section, it runs just fine. Any thoughts on how I can get this to work? Is Excel being overloaded?

(I'm not great with VBA, but what i'm trying to do is select the pivot table in the active sheet, have the sum of "cost" added to the values section of the pivot, and then format it. Then, have the sum of "Impressions" added to the value section of the pivot, and then format it.)

Sub PivotFormat()
Dim pf As PivotField


ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).AddDataField ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).PivotFields("Cost"), "Sum of Cost", xlSum
Set pf = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).PivotFields("Sum of Cost")
pf.DataRange.NumberFormat = "_($* #,##0.00"


ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).AddDataField ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).PivotFields("Impressions"), "Sum of Impressions", xlSum
Set pf = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name).PivotFields("Sum of Impressions")
pf.DataRange.NumberFormat = "#,##0"

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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