我需要在Excel的输入框中过滤用户输入值的数据透视表。如何根据我在LYear中使用的用户输入值过滤此数据透视表。以下是我的代码无效
Sub LoopThroughPivotItems()
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem
Set PT = ActiveSheet.PivotTables("PivotTable4")
'Set PF = PT.PivotFields("2013")
LYear = InputBox("Enter Year")
'PF.Value = LYear
ActiveSheet.PivotTables("PivotTable4").PivotFields("2012").CurrentPage = LYear
'PF.AutoShowField = LYear
'PF.AutoShowField = "LYear"
'For Each PI In PF.PivotItems
'PI.Visible = True ' Error is occuring here
'Next PI
If PI = PF.PivotItems Then
PI.Visible = True
Else: PI.Visible = False
End If
End Sub
答案 0 :(得分:1)
lYear = InputBox("Enter Year")
ActiveSheet.PivotTables("PivotTable4").PivotFields("Year").PivotItems(lYear).Visible = True
For Each pit In ActiveSheet.PivotTables("PivotTable1").PivotFields("Year").PivotItems
If pit.Value <> lYear Then
pit.Visible = False
End If
Next pit