卓越的VBAfilter枢轴表灵活的名称

Set Weight_wb = Application.Workbooks(WEIGHTFileName).Sheets("TERRY_CAT") Set pt = Weight_wb .PivotTables("PivotTable4") With Weight_wb .PivotTables("PivotTable4").PivotFields("Cat") .Orientation = xlColumnField .Position = 1 End With With Weight_wb .PivotTables("PivotTable4").PivotFields("Cat") .Orientation = xlColumnField On Error Resume Next .PivotItems("black").Visible = True .PivotItems("yellow").Visible = False .PivotItems("brown").Visible = False .Position = 1 End With 

我想改变上面的代码更加灵活。 我想写一个更一般的子SetClolumnFilter,以便我可以简单地调用:

 SetColumnFilter WEIGHTFileName, "Terry_cat", "PivotTable4", "CAT","black" 

像(它不工作)的东西:

 Sub SetPageFilter(WB As String, WS As String, pt As String, fd As String,value as string) Dim wb_ As Workbook, ws_ As Worksheet, pt_ As PivotTable, fd_ As PivotField ws_ = Application.Workbooks(WB).Sheets(WS) pt_ = ws_.PivotTables(pt) fd_ = pt_.PivotFields(fd) With fd_ .Orientation = xlPageField End With Application.ScreenUpdating = True Dim i As Long .PivotItems(1).Visible = True For i = 2 To Field.PivotItems.Count If .PivotItems(i).Name = Value Then _ .PivotItems(i).Visible = True Else _ .PivotItems(i).Visible = False Next i End Sub 

非常感谢!!

 Sub SetPageFilter(WB As String, WS As String, pt As String, fd As String,value as string) Dim wb_ As Workbook, ws_ As Worksheet, pt_ As PivotTable, fd_ As PivotField ws_ = Application.Workbooks(WB).Sheets(WS) pt_ = ws_.PivotTables(pt) fd_ = pt_.PivotFields(fd) With fd_ .Orientation = xlPageField End With Application.ScreenUpdating = True Dim i As Long With fd_ .PivotItems(.PivotItems.Count).Visible = True For i = 1 To .PivotItems.Count If .PivotItems(i).Name = Value Then _ .PivotItems(i).Visible = True Else _ .PivotItems(i).Visible = False Next I End With End Sub