Loop Through Pivot Tables And Remove Same Values

Excelate

New Member
Joined
Jan 5, 2017
Messages
11
Hi All,

I am trying to loop through all pivot tables in a sheet and remove all values fields in them which have the same name: "Total Net Spend" and "% Split".

kUAy8Tc.png


I am trying the below code but it will only work on the first pivot and won't loop through all of them. How do I edit the code so that it will remove "Total Net Spend" and "% Split" columns on all the pivot tables in the sheet?


Sub Loop_Pivots()


Dim PT As PivotTable, PTField As PivotField
Set PT = Sheets("Sheet1").PivotTables("Pivot1")
With PT
.ManualUpdate = True
For Each PTField In .DataFields
PTField.Orientation = xlHidden
Next PTField
.ManualUpdate = False
End With
Set PT = Nothing


End Sub


Thanks,
Andrea
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hello,

The macro you are showing ... does not loop ... !!! :wink:

Have you tried to insert For Each PT in Activesheet.PivotTables ...

HTH
 
Upvote 0
Thanks James. Sorry, I only have started using Macros so not sure how to go about this... I tried to add your bit to the code below but it doesn't work - any idea why?

Sub Loop_Pivots()


Dim PT As PivotTable, PTField As PivotField
Set PT = Sheets("Sheet1").PivotTables("Pivot1")
With PT
.ManualUpdate = True
For Each PT In ActiveSheet.PivotTables
For Each PTField In .DataFields
PTField.Orientation = xlHidden
Next PTField
Next PT
.ManualUpdate = False
End With
Set PT = Nothing


End Sub
 
Upvote 0
No problem ...:wink:

Code:
[COLOR=#333333]Sub Loop_Pivots()[/COLOR]
[COLOR=#333333]Dim PT As PivotTable
Dim PTField As PivotField[/COLOR]
[COLOR=#333333]For Each PT In ActiveSheet.PivotTables[/COLOR]
[COLOR=#333333].ManualUpdate = True[/COLOR]
[COLOR=#333333]     For Each PTField In .DataFields[/COLOR]
[COLOR=#333333]        PTField.Orientation = xlHidden[/COLOR]
[COLOR=#333333]     Next PTField[/COLOR]
[COLOR=#333333] .ManualUpdate = False[/COLOR][COLOR=#333333]
Next PT[/COLOR]

[COLOR=#333333]End Sub[/COLOR]

HTH
 
Upvote 0
Once you have all your pivot tables in place ... you can test following

Code:
Sub Loop_Pivots()
Dim PT As PivotTable
Dim PTField As PivotField
For Each PT In ActiveSheet.PivotTables
    For Each PTField In .DataFields
       PTField.Orientation = xlHidden
    Next PTField
Next PT
End Sub

HTH
 
Upvote 0
Still giving me the same error (invalid error or unqualified reference) this time on .DataFields. My original code does the job (gets rid of values fields) however, as you said, doesn't loop through all the pivot tables in the sheet. what would the code be for doing just that? For now it just does the job on the first pivot but then stops - need to understand how to make it loop until it finds pivot tables in the sheet...
 
Upvote 0
The Loop Instruction is :

For Each ... In ActiveSheet.PivotTables
 
Upvote 0
I see... Then I have no idea why the code isn't working. If I use the below it will give me an error message about .DataFields command being an invalid error or unqualified reference...
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,287
Members
449,149
Latest member
mwdbActuary

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