banneduser123
Banned - Rules violations
- Joined
- Mar 29, 2006
- Messages
- 181
hi, i'm using the below code to add fields to my pivot table. it works great, but my problem is it only works for one pivot table on each page.
can someone help me edit this so that if there are multiple pivots on the same sheet?
if it helps, i'm just using the code this guy created in the "Pivot Table Fields Macro Buttons.xlsm" file here:
https://www.excelcampus.com/vba/macro-buttons-pivot-table-fields/
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
can someone help me edit this so that if there are multiple pivots on the same sheet?
if it helps, i'm just using the code this guy created in the "Pivot Table Fields Macro Buttons.xlsm" file here:
https://www.excelcampus.com/vba/macro-buttons-pivot-table-fields/
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