过滤用户输入的数据透视表

时间:2014-12-09 06:12:27

标签: excel vba excel-vba

我需要在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

1 个答案:

答案 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