I recorded this macro.
But, i can't get it to loop over all the fields in my PivotTable, all of which i want to change to "Average" (about 25 fields)
Of course the Caption would have to change as well, i understand that. I have tried various solutions and none of them work. The loops seem to choke over the ".formula" step, with a VBA error.
I have done this in the immediate window, after changing the field where the macro was recorded back to Sum. Note that this PivotField is PivotField(1).
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").name
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").Caption
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").Formula
-4157
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).name
SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).Caption
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).Formula
=> VBA Err msgbox pops up, nothing displayed in Immediate window.
Why does PivotFields(1).name give a different result than PivotFields("SiEv2P.a").name?
Why can't I get the Formula property in the immediate window for this PivotField, whether i represent it by its Name or by its field index? [Bad ramifications in trying to write code in a loop]
This is not just a theoretical question, as when i am trying to create a loop, i cannot create one that works.
If you can create a loop that will work for PivotField index 1 to 25, please post. I can figure out how to change the Caption text to something unique in each loop.
Thanks!
VBA Code:
Sub Macro5()
'
' Macro5 Macro
'
'
Range("A4").Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a")
.Caption = "Average of SiEv2P.a"
.Function = xlAverage
.NumberFormat = "0.000"
End With
End Sub
But, i can't get it to loop over all the fields in my PivotTable, all of which i want to change to "Average" (about 25 fields)
Of course the Caption would have to change as well, i understand that. I have tried various solutions and none of them work. The loops seem to choke over the ".formula" step, with a VBA error.
I have done this in the immediate window, after changing the field where the macro was recorded back to Sum. Note that this PivotField is PivotField(1).
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").name
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").Caption
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of SiEv2P.a").Formula
-4157
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).name
SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).Caption
Sum of SiEv2P.a
?ActiveSheet.PivotTables("PivotTable2").PivotFields(1).Formula
=> VBA Err msgbox pops up, nothing displayed in Immediate window.
Why does PivotFields(1).name give a different result than PivotFields("SiEv2P.a").name?
Why can't I get the Formula property in the immediate window for this PivotField, whether i represent it by its Name or by its field index? [Bad ramifications in trying to write code in a loop]
This is not just a theoretical question, as when i am trying to create a loop, i cannot create one that works.
If you can create a loop that will work for PivotField index 1 to 25, please post. I can figure out how to change the Caption text to something unique in each loop.
Thanks!
Last edited by a moderator: