I am attempting to set up a macro that will cycle through the worksheets in my file and update my pivot tables.
The below code will cycle through each worksheet and each pivot table:
Dim wks As Worksheet
Dim pvt As PivotTable
For Each wks In Worksheets
For Each pvt In wks.PivotTables
Next pvt
Next wks
the next step is where I'm having issues. I want to update two filters on each pivot table (all the same) . I attempted to record a macro to figure out how to get it done, but can't figure out how to incorporate it into my for /next loops:
ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName1").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName1")
.PivotItems("Name1").Visible = True
.PivotItems("Name2").Visible = True
.PivotItems("Name3").Visible = True
.PivotItems("Name4").Visible = True
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("FilterName2").CurrentPage = "(All)"
With ActiveSheet.PivotTables(pvt).PivotFields("FilterName2")
.PivotItems("Item1").Visible = True
.PivotItems("Item2").Visible = True
.PivotItems("Item3").Visible = True
.PivotItems("Item4").Visible = True
.PivotItems("Item5").Visible = True
End With
I get the error "Method 'PivotTables' of object'_worksheet' failed." when I try the below code:
Dim wks As Worksheet
Dim pvt As PivotTable
For Each wks In Worksheets
For Each pvt In wks.PivotTables
wks.PivotTables(pvt).PivotFields("PO Type").CurrentPage = _
"(All)"
With wks.PivotTables(pvt).PivotFields("FilterName1")
.PivotItems("Name1").Visible = True
.PivotItems("Name2").Visible = True
.PivotItems("Name3").Visible = True
.PivotItems("Name4").Visible = True
End With
wks.PivotTables(pvt).PivotFields("SBU").CurrentPage = "(All)"
With wks.PivotTables(pvt).PivotFields("FilterName2")
.PivotItems("Item1").Visible = True
.PivotItems("Item2").Visible = True
.PivotItems("Item3").Visible = True
.PivotItems("Item4").Visible = True
.PivotItems("Item5").Visible = True
End With
Next pvt
Next wks
any guidance is appreciated.
Bookmarks