VBA macro to format Pivot table fields

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
I am trying to write a macro which would format all pivot table fields that start with "Count of" as a number and all that start with "Sum of" as currency.

This is the code I have so far, but I doesn't quite work:

Sub FormatPivot1()
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(1)

pt.PivotFields("Count of " & "*").NumberFormat = "###0"
pt.PivotFields("Sum of " & "*").NumberFormat = "$ #,##0"

Range("B26").Select
End Sub

Does anyone know the proper syntax that would make it work?
Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try...

Code:
Sub FormatDataFields()

    Dim pf As PivotField
    
    For Each pf In ActiveSheet.PivotTables(1).DataFields
        Select Case pf.Function
            Case -4112 'xlCount
                pf.NumberFormat = "###0"
            Case -4157 'xlSum
                pf.NumberFormat = "$ #,##0"
        End Select
    Next pf
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,184
Members
448,949
Latest member
keycalinc

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