简体   繁体   English

如何使用 VBA 过滤一个或多个数据透视表字段?

[英]How do I Filter one or multiple PivotTable Field using VBA?

I have a workbook with multiple pivot tables.我有一个带有多个数据透视表的工作簿。 I have been writing macros to filter a specific field ("YEAR_FW") on the pivot tables based on cell range that lives on a different sheet.我一直在编写宏来根据位于不同工作表上的单元格范围过滤数据透视表上的特定字段(“YEAR_FW”)。 After a bunch of trial and error I decided to write separate code for each Pivot table.经过大量试验和错误后,我决定为每个数据透视表编写单独的代码。 I was able to filter most of the pivot tables but there are some that give me an error.我能够过滤大部分数据透视表,但有些数据给我一个错误。

Here is the cell range:这是单元格范围:

enter image description here在此处输入图像描述

Here are the pivot tables(the first one got filtered):这是数据透视表(第一个已过滤):

enter image description here在此处输入图像描述

The first table code that worked:第一个有效的表格代码:

Sub Filter_dip4wk()

Dim rng As Range
Set rng = Range("B3:B58")
Dim ptb As PivotTable
Set ptb = Sheets("DIP COAT TABLE 4week").PivotTables("PivotTable1")
Dim fld As PivotField
Set fld = ptb.PivotFields("YEAR_FW")
           
    With fld

    Dim Item As PivotItem

    For Each Item In .PivotItems
     Item.Visible = True
    Next Item
     
    For Each Item In .PivotItems
        Item.Visible = False
        Dim cell As Range
            For Each cell In rng
                If Item.Caption = cell.Text Then
                    Item.Visible = True
                    Exit For
                End If
            Next cell
    Next Item

    End With

End Sub

The second table code and error (basically just changing pivot table name):第二个表代码和错误(基本上只是更改数据透视表名称):

Sub Filter_dip4wkt2()

Dim rng As Range
Set rng = Range("B3:B58")
Dim ptb As PivotTable
Set ptb = Sheets("DIP COAT TABLE 4week").PivotTables("PivotTable2")
Dim fld As PivotField
Set fld = ptb.PivotFields("YEAR_FW")
           
    With fld

    Dim Item As PivotItem

    For Each Item In .PivotItems
     Item.Visible = True
    Next Item
     
    For Each Item In .PivotItems
        Item.Visible = False
        Dim cell As Range
            For Each cell In rng
                If Item.Caption = cell.Text Then
                    Item.Visible = True
                    Exit For
                End If
            Next cell
    Next Item

    End With

End Sub

enter image description here在此处输入图像描述

I think it is because the second pivot table has a row field.我认为这是因为第二个数据透视表有一个行字段。 I tried making changed to PivotItem part but still got errors.我尝试对 PivotItem 部分进行更改,但仍然出现错误。 Any ideas?有任何想法吗? I am new to VBA/Macros so apologies if I missed out any details.我是 VBA/宏的新手,如果我错过了任何细节,我深表歉意。

Try something like this:尝试这样的事情:

Sub Filter_dip4wkt2()

    Dim rng As Range, fld As PivotField, itm As PivotItem
    
    Set rng = Range("B3:B58") 'ideally specify a worksheeet here...
    
    Set fld = Sheets("DIP COAT TABLE 4week").PivotTables("PivotTable2").PivotFields("YEAR_FW")
           
    fld.ClearAllFilters 'first set all visible
    
    For Each itm In fld.PivotItems
        'look for the caption in rng
        If IsError(Application.Match(itm.Caption, rng, 0)) Then
            itm.Visible = False 'hide non-matches
        End If
    Next itm
    
End Sub

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

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