简体   繁体   English

Excel VBA - 过滤数据透视表中的特定数据

[英]Excel VBA - Filter specific data in Pivot Table

I tried using the code that appeared to have helped the poster at 我尝试使用似乎帮助海报的代码

Excel VBA - Privot table filter multiple criteria Excel VBA - Privot表筛选多个条件

But I keep on getting the following error: Unable to get the PIvtoFields property of the PIvotTable Class. 但我继续收到以下错误:无法获取PIvotTable类的PIvtoFields属性。

Sub FilterPivotItems()

    Dim PT          As PivotTable
    Dim PTItm       As PivotItem
    Dim FiterArr()  As Variant

    ' use an array to select the items in the pivot filter you want to keep visible
    FiterArr = Array("MC. Santa Clara", "MC. Plaza Américas", "MC. El Frutal")

    ' set the Pivot Table
    Set PT = ActiveSheet.PivotTables("PivotTable4")

    ' loop through all Pivot Items in "Value" Pivot field
    For Each PTItm In PT.PivotFields("Value").PivotItems
        If Not IsError(Application.Match(PTItm.Caption, FiterArr, 0)) Then ' check if current item is not in the filter array
            PTItm.Visible = True
        Else
            PTItm.Visible = False
        End If
    Next PTItm

End Sub

What can I do? 我能做什么?

After reading about the properties of the class, I finally got to what I wanted. 在阅读了课程的属性之后,我终于得到了我想要的东西。

 Sub filterpivot() Dim pi As PivotItem Dim pt As PivotTable Dim first As String Dim second As String Dim third As String Dim fourth As String first = Range("a26").Value second = Range("a27").Value third = Range("a28").Value fourth = Range("a29").Value Set pt = Worksheets("Lowest Scores").PivotTables("PivotTable4") With pt.PivotFields("Nombre conjunto") For Each pi In pt.PivotFields("Nombre conjunto").PivotItems If pi.Name = first Or pi.Name = second Or pi.Name = third Or pi.Name = fourth Then pi.Visible = True Else pi.Visible = False End If Next pi End With End Sub 

This bases the applied filter on specific cell values. 这将应用的过滤器基于特定的单元格值。 My apologies if anyone was working on this already, I was on a crunch at work and needed the answer. 我很抱歉,如果有人正在研究这个问题,我工作紧张,需要答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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