我当前的代码
With PT.PivotFields("Year")
.PivotItems("2012").Visible = False
.PivotItems("2013").Visible = False
.PivotItems("2014").Visible = False
.PivotItems("2011").Visible = False
.PivotItems("2015").Visible = False
End With
有没有办法将变量=设置为当前年份,以便我可以使用If / Then语句来格式化此数据透视表?
答案 0 :(得分:1)
学习使用For
循环。
With PT.PivotFields("Year")
For yr = 2011 To 2015
.PivotItems(CStr(yr)).Visible = False
Next
End With
答案 1 :(得分:1)
看看是否有效
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set pt = ActiveSheet.PivotTables(1)
' change field as needed
Set pf = pt.PivotFields("Year")
For Each pi In pf.PivotItems
If pf.Value <> "2016" Then pi.Visible = False
Next pi