VBA to format Pivot Table measure (PowerPivot) not working

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
145
Hi,

I have a data model which I use to create a pivot table. I am using multiple measures in the same pivot table ($'s and %'s).
The one of interest can be referenced in my code as such: "ActiveSheet.PivotTables("PivotTable2").CubeFields("[Measures].[YTD % Chg (DS)]")."

I need a code to automatically change the number format of that measure to "0%;[Red]-0%" in my pivot table.
This needs to be in VBA because I created buttons that allow report users to toggle from one product sales info to another. I want the % chg year over year to formatted every time the pivot table is changed.

When I use the macro recorder, the following code is generated, but it doesn't work when I try to run it. I don't understand why.
VBA Code:
    With ActiveSheet.PivotTables("PivotTable2").PivotFields( _
        "[Sell Out].[Chain Category].[Chain Category]")
        PivotItems().Visible = "0%;[Red]-0%"
    End With
End Sub

Could you please help? Any guidance would be appreciated.
Thank you.
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,145
Members
415,881
Latest member
tasic89

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
Top