I have created a pivot table with vba that specifies fields such as:
With PT
.PivotFields("Billing").Orientation = xlRowField
.PivotFields("Billing").Position = 1
.PivotFields("Billing").Subtotals(1) = False
.PivotFields("Supplier").Orientation = xlRowField
.PivotFields("Supplier").Position = 2
.PivotFields("Supplier").Subtotals(1) = False
.PivotFields("Year").Orientation = xlColumnField
.PivotFields("Year").Position = 1
.PivotFields("Year").Subtotals(1) = False
.PivotFields("Month").Orientation = xlColumnField
.PivotFields("Month").Position = 2
.PivotFields("Month").Subtotals(1) = False
.PivotFields("Total").Orientation = xlDataField
.PivotFields("Total").Subtotals(1) = False
.RowAxisLayout xlTabularRow
.RepeatAllLabels xlRepeatLabels
End With
For any PivotFields("Year").PivotItems less than the year prior to the current year, I want to hide.
I used the recorder and got:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Year")
.PivotItems("2017").Visible = False
.PivotItems("2018").Visible = False
.PivotItems("2019").Visible = False
.PivotItems("2020").Visible = False
.PivotItems("2021").Visible = False
End With
...but I don't know how to dynamically execute it with the creation of the PivotTable.
With PT
.PivotFields("Billing").Orientation = xlRowField
.PivotFields("Billing").Position = 1
.PivotFields("Billing").Subtotals(1) = False
.PivotFields("Supplier").Orientation = xlRowField
.PivotFields("Supplier").Position = 2
.PivotFields("Supplier").Subtotals(1) = False
.PivotFields("Year").Orientation = xlColumnField
.PivotFields("Year").Position = 1
.PivotFields("Year").Subtotals(1) = False
.PivotFields("Month").Orientation = xlColumnField
.PivotFields("Month").Position = 2
.PivotFields("Month").Subtotals(1) = False
.PivotFields("Total").Orientation = xlDataField
.PivotFields("Total").Subtotals(1) = False
.RowAxisLayout xlTabularRow
.RepeatAllLabels xlRepeatLabels
End With
For any PivotFields("Year").PivotItems less than the year prior to the current year, I want to hide.
I used the recorder and got:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Year")
.PivotItems("2017").Visible = False
.PivotItems("2018").Visible = False
.PivotItems("2019").Visible = False
.PivotItems("2020").Visible = False
.PivotItems("2021").Visible = False
End With
...but I don't know how to dynamically execute it with the creation of the PivotTable.