waverider80
New Member
- Joined
- Jan 9, 2010
- Messages
- 3
Hi everybody!
First off, I'd like to thank all of you for this incredible forum... it has helped me countless times in my work life!!
I have a simple pivot table with a few calculated fields. When I add the calculated fields to the pivot table, two issues occur:
Instead, what I'd like to happen is if a user drags a pivot field from the PivotTable Field List onto the Pivot Table, then I'd like it to:
A couple things I've figured out... (please tell me if I'm wrong):
Sub UpatePivotFieldNames()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveSheet.PivotTables(1)
For Each pf In pt.PivotFields
MsgBox pf.Caption
Next pf
End Sub
How can I get this code to only run through the fields on the pivot table in the sheet itself and not the FieldList? Also is there an event for when someone adds a field to the pivot table?
FYI: Using Excel 2010, Windows XP (work computer)
THANK YOU!!!! You guys are awesome!
First off, I'd like to thank all of you for this incredible forum... it has helped me countless times in my work life!!
I have a simple pivot table with a few calculated fields. When I add the calculated fields to the pivot table, two issues occur:
- The caption is renamed: Sum of [field]
- The formatting reverts to a default
Instead, what I'd like to happen is if a user drags a pivot field from the PivotTable Field List onto the Pivot Table, then I'd like it to:
- Automatically change the caption to a user friendly field name that I create (basically the name without "Sum of" and...
- Change the formatting (i.e. a percentage would be 0.0%)
A couple things I've figured out... (please tell me if I'm wrong):
- I'm going to place the code in the Sheet 1 Object of the VBA Project under the PivotTableChangeSync event so that it will check this code everytime a change is made to the pivot table. (Ideally, I'd like it to only check/run the macro if a field is added to the pivot table but can't find this event). Is this correct?
- I'm going to use a For each Next loop to loop through all the pivot fields on the current pivot table
- I'll use a case if statement to check which field it is and then if it's field A then change the caption and formatting to xx, and if field B then xx, etc. This part I've got down (by using the Macro Recorder)
- Problem: I've put together the below code just to loop through the fields, but it seems to loop through all the fields that are listed in the PivotTable Field List rather than only the ones on the pivot table residing on the actual sheet. How can I loop through just the ones on the sheet?
Sub UpatePivotFieldNames()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveSheet.PivotTables(1)
For Each pf In pt.PivotFields
MsgBox pf.Caption
Next pf
End Sub
How can I get this code to only run through the fields on the pivot table in the sheet itself and not the FieldList? Also is there an event for when someone adds a field to the pivot table?
FYI: Using Excel 2010, Windows XP (work computer)
THANK YOU!!!! You guys are awesome!