vba to add fields to pivot table, needs work

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
 
Before running the macro try to check if you can do it manually.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
hi Dante,

i am able to, maybe i should clarify...

it's kinda working, but in an odd way...
i'm able to press the buttons, and the fields will be added or be hidden, but the error will pop up alongside. it's occuring at the 2nd point it's used in the code..under the "Else Add to Rows area" :



Else 'Add to Rows area
pt.PivotFields(sField).Orientation = xlRowField
shp.Fill.ForeColor.Brightness = 0
 
Upvote 0
I really don't know how to hide the fields, all I did was put your code in a cycle.
But you must fine tune it with a table and then adjust it in the cycle.
 
Upvote 0
The error is new, or did you already have it from the beginning?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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