Hi all,
I'm currently using this code to toggle pivot table row fields, there are buttons on my spreadsheet that I click to add/remove row fields in the pivot table.
This works great, however I was wandering if it is possible to adapt the code to toggle pivot table data field "values" by average.
I've played around with the code and i can get it to semi work.
I changed all the Rowfields to Datafields however the data just duplicates over and over and doesn't remove the previous field selection.
As you can probably see I am a complete novice to VBA, any help would be much appreciated. I have been scouring the net for answers with no joy so after a day of scratching my head I'm looking for someone to point me in the right direction.
Thank you in advance.
Sub Toggle_Row_Field()
'Add/Remove the field to the pivot table.
'The field is determined by the button text that calls the macro.
Dim pt As PivotTable
Dim pf As PivotField
Dim sField As String
Dim shp As Shape
'Set variables
Set pt = ActiveSheet.PivotTables(1)
Set shp = ActiveSheet.Shapes(Application.Caller)
sField = shp.TextFrame.Characters.Text
'Toggle field
'If visible then hide it
If pt.PivotFields(sField).Orientation = xlRowField Then
pt.PivotFields(sField).Orientation = xlHidden
shp.Fill.ForeColor.Brightness = 0.5
Else 'Add to Rows area
pt.PivotFields(sField).Orientation = xlRowField
shp.Fill.ForeColor.Brightness = 0
End If
End Sub
I'm currently using this code to toggle pivot table row fields, there are buttons on my spreadsheet that I click to add/remove row fields in the pivot table.
This works great, however I was wandering if it is possible to adapt the code to toggle pivot table data field "values" by average.
I've played around with the code and i can get it to semi work.
I changed all the Rowfields to Datafields however the data just duplicates over and over and doesn't remove the previous field selection.
As you can probably see I am a complete novice to VBA, any help would be much appreciated. I have been scouring the net for answers with no joy so after a day of scratching my head I'm looking for someone to point me in the right direction.
Thank you in advance.
Sub Toggle_Row_Field()
'Add/Remove the field to the pivot table.
'The field is determined by the button text that calls the macro.
Dim pt As PivotTable
Dim pf As PivotField
Dim sField As String
Dim shp As Shape
'Set variables
Set pt = ActiveSheet.PivotTables(1)
Set shp = ActiveSheet.Shapes(Application.Caller)
sField = shp.TextFrame.Characters.Text
'Toggle field
'If visible then hide it
If pt.PivotFields(sField).Orientation = xlRowField Then
pt.PivotFields(sField).Orientation = xlHidden
shp.Fill.ForeColor.Brightness = 0.5
Else 'Add to Rows area
pt.PivotFields(sField).Orientation = xlRowField
shp.Fill.ForeColor.Brightness = 0
End If
End Sub
Last edited: