簡體   English   中英

具有靈活名稱的Excel VBA篩選器透視表

[英]excel VBA filter pivot table with flexible names

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM