VBA: Use macro to automatically format value field settings of pivot table to a single value

nflami

New Member
Joined
Apr 16, 2014
Messages
25
Hello all,

Currently I have the below code which is successful at formatting all the value field settings in the selected pivot table to my specified format. My goal is to be able to apply the same macro, but to the single selected value field, rather than all the value fields.

Thanks for your help!

Code:
Public Sub SetDataFieldsToSum()

' Nick

   Dim ptf               As PivotField
   With Selection.PivotTable
      .ManualUpdate = True
      For Each ptf In .DataFields
         With ptf
            .Function = xlSum
            .NumberFormat = "#,##0_);[Red](#,##0)"
         End With
      Next ptf
      .ManualUpdate = False
   End With
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi nflami,

If you know the name of the data field, you can reference it directly like this...

Code:
   Dim ptf  As PivotField

   With Selection.PivotTable
      .ManualUpdate = True
      With .DataFields("Sum of Sales")
         .Function = xlSum
         .NumberFormat = "#,##0_);[Red](#,##0)"
      End With
      .ManualUpdate = False
   End With
 
Upvote 0
Hi Jerry,

Thank you! This is awesome and will be very helpful. I would like to take this a step further if possible. Is it possible to reference the data field by the current cursor location. So if I was in the cell titled "Sum of Sales" it would then execute the macro to the data field? That way I could used this across multiple fields based on the format I want.

Thanks again,
Nick
 
Upvote 0
I don't really understand how to write macros with a Dim so the below code doesn't work, but may this will help you to see what I'm hoping for. Based on the text in the active cell, I want apply that to the data field.

Code:
Public Sub SetSingleDataFieldsToSum()

   Dim ptf  As PivotField
   Dim name  As String
   name = ActiveCell.Value
   

   With Selection.PivotTable
      .ManualUpdate = True
      With .DataFields("String")
         .Function = xlSum
         .NumberFormat = "#,##0_);[Red](#,##0)"
      End With
      .ManualUpdate = False
   End With

End Sub

Thanks!
 
Upvote 0
You were close. It would work if you changed it to:

Code:
With .DataFields(name)

I'd suggest a couple more modifications:

It's best to avoid using keywords like "name" that have other meanings in VBA as variable names.

If you reference ActiveCell.PivotField.Name then you can run this macro after having selected any DataValue in that field- not just the heading.

Here's a modified version...

Code:
Public Sub SetSingleDataFieldsToSum()

   Dim ptf  As PivotField
   Dim sFieldName  As String
   
   sFieldName = ActiveCell.PivotField.Name
   
   With Selection.PivotTable
      .ManualUpdate = True
      With .DataFields(sFieldName)
         .Function = xlSum
         .NumberFormat = "#,##0_);[Red](#,##0)"
      End With
      .ManualUpdate = False
   End With

End Sub
 
Upvote 0
[SOLVED] Re: VBA: Use macro to automatically format value field settings of pivot table to a single value

Thank you, exactly what I was looking for. I appreciate your help and the pointers.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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