简体   繁体   中英

VBA single select on a Pivot Table field filter

I am working on my VBA code for a PivotTable field.

What I want to achieve is to only select Acc Payable in field Group . The following code can help me get what I want, but I'm considering whether there is a way to delete those False lines and make the code shorter?

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Group")
        .PivotItems("Acc Services").Visible = False
        .PivotItems("FRG").Visible = False
        .PivotItems("Non FinOps").Visible = False
        .PivotItems("Semi Auto").Visible = False
        .PivotItems("Acc Payable").Visible = True
    End With
        End With

You can use a For loop, to iterate through the PivotField named "Group" PivotItems , and if the PivotItem.Name = "Acc Payable" then make it visible.

Code

Dim PvtItm As PivotItem

For Each PvtItm In ActiveSheet.PivotTables("PivotTable1").PivotFields("Group").PivotItems
    If PvtItm.Name = "Acc Payable" Then
        PvtItm.Visible = True
    Else
        PvtItm.Visible = False
    End If
Next PvtItm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM