I have 16 pivot tables that I need to update when the user presses a button. The code that needs to run is as follows:
ActiveSheet.PivotTables("PivotTable10").PivotFields("count").ClearAllFilters
ActiveSheet.PivotTables("PivotTable10").PivotFields("scrap code").ClearAllFilters
ActiveSheet.PivotTables("PivotTable10").PivotFields("count").ShowAllItems = True
With ActiveSheet.PivotTables("PivotTable10").PivotFields("count")
.PivotItems("0").Visible = False
End With
The names of the pivot tables are:
MSP,MSP30,FSP,FSP30,MRP,MRP30,FRP,FRP30,MPP,MPP30,FPP,FPP30,MCP,MCP30,FCP,FCP30
I'd like to replace PivotTable10
with a variable that loops thru that list of pivot tables. Right now my code is 16 blocks of the code above. I know little about loops and I haven't found a good example of this type of loop in my google searches.
EDIT: finally code that worked, both answers below worked perfectly, this code just worked slightly faster
Sub IteratePivots()
Application.ScreenUpdating = False
On Error Resume Next
Worksheets("Analytics Admin").Activate
Dim pvtTables As PivotTables
Dim pvtTable As PivotTable
Set pvtTables = Application.ActiveSheet.PivotTables
For Each pvtTable In pvtTables
pvtTable.PivotFields("count").ClearAllFilters
pvtTable.PivotFields("scrap code").ClearAllFilters
pvtTable.PivotFields("count").ShowAllItems = True
With pvtTable.PivotFields("count")
.PivotItems("0").Visible = False
End With
Next
Application.ScreenUpdating = False
End Sub