简体   繁体   English

Excel VBA-具有多个值过滤器的数据透视表

[英]Excel VBA - Pivot Table with Multiple Value Filters

Is there a way in Excel VBA to allow for multiple value filters in a Pivot Table? Excel VBA中是否有一种方法可以在数据透视表中使用多个值过滤器? I'm trying to filter values for >0 AND for Bottom Ten items. 我正在尝试过滤> 0和下十项的值。 In the sample code below (generated by recording a Macro), the second filter step overrides the first. 在下面的示例代码(通过记录宏生成)中,第二个过滤器步骤将覆盖第一个步骤。 If multiple value filters were supported, it seems I would only need to add the Boolean logic AND between these two expressions to get the product of both filters. 如果支持多个值过滤器,似乎只需要在这两个表达式之间添加布尔逻辑AND即可获得两个过滤器的乘积。 Please suggest any changes to the code or let me know if this is not supported. 请建议对代码进行任何更改,或者如果不支持,请通知我。 Thanks! 谢谢!

Sub Multiple_Value_Filters()

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Full Name").PivotFilters.Add _       
        Type:=xlValueIsGreaterThan, _
        DataField:=ActiveSheet.PivotTables("PivotTable1").PivotFields("Days"), _
        Value1:=0
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Full Name").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Full Name").PivotFilters.Add _
        Type:=xlBottomCount, _
        DataField:=ActiveSheet.PivotTables("PivotTable1").PivotFields("Days"), _
        Value1:=10
End Sub

I can't really tell what your pivottable should be doing without seeing some sample data but I believe you should be able to do what you are trying to. 在没有看到一些样本数据的情况下,我无法真正说出您的数据透视表应该做什么,但是我相信您应该能够做您想做的事情。 Try messing around with this: 尝试解决这个问题:

Sub Multiple_Value_Filters()
    Dim pvt As PivotTable
    Set pvt = ActiveSheet.PivotTables("PivotTable1")

    With pvt.PivotFields("Full Name")
        .ClearAllFilters
        .PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=pvt.PivotFields("Days"), Value1:=0
        .PivotFilters.Add Type:=xlBottomCount, DataField:=pvt.PivotFields("Days"), Value1:=10
    End With
End Sub

Have discovered the PivotTable Option to allow multiple filters, however it didn't quite work for me even when it would work as I manually did it. 已经发现PivotTable选项可以使用多个过滤器,但是即使它可以像我手动执行的那样对我也不起作用。 For whatever reason Excel seems to not like the code. 无论出于何种原因,Excel似乎都不喜欢该代码。 This functionality only works for Excel 2007 PivotTables and newer but I am running mine from Excel 2010 so I am not sure what the issue is here. 该功能仅适用于Excel 2007 PivotTables和更高版本,但是我正在Excel 2010中运行我的软件,因此我不确定这里存在什么问题。

Sub Multiple_Value_Filters()
    Dim pvt As PivotTable
    Set pvt = ActiveSheet.PivotTables("PivotTable1")

    With pvt.PivotFields("Full Name")
        .ClearAllFilters
        .AllowMultipleFilters = True ' This is the main key to getting this to work but mine still errors out whenever I add the 2nd filter. 
        .PivotFilters.Add Type:=xlValueIsGreaterThan, DataField:=pvt.PivotFields("Days"), Value1:=0
        .PivotFilters.Add Type:=xlBottomCount, DataField:=pvt.PivotFields("Days"), Value1:=10
    End With
End Sub

I know this question is already quite old, but I stumbled upon it recently and nothing was really working for me. 我知道这个问题已经很老了,但是最近我偶然发现了这个问题,对我来说真的没有任何作用。 I could only use one filter for the pivot table. 我只能对透视表使用一个过滤器。 For the second or third I always got Run Time Errors. 对于第二个或第三个,我总是遇到运行时错误。 However, I found a simple workaround, which I will describe below. 但是,我找到了一个简单的解决方法,下面将对其进行描述。 I hope other people coming here with similar issues might find it helpful. 我希望其他来这里遇到类似问题的人可能会有所帮助。

Step 1: Add Helper Columns to the data source with a new heading and any constant value in every row. 步骤1:将Helper Columns添加到数据源,并带有新的标题和每行中的任何常量值。 (You need one helper column per extra filter. If you want to use 3 filters, you need two helper columns) (每个额外的过滤器需要一个辅助列。如果要使用3个过滤器,则需要两个辅助列) 在此处输入图片说明

Step 2: Add the Helpercolumn attributes to your row-fields of the pivot table. 步骤2:将Helpercolumn属性添加到数据透视表的行字段中。 在此处输入图片说明

Step 3: Choose the tabular layout where all row attributes are in one row. 步骤3:选择所有行属性都在一行中的表格布局。 在此处输入图片说明

Step 4: Now you can apply different filters, one for each attribute in the row-field. 步骤4:现在您可以应用不同的过滤器,对行字段中的每个属性应用一个。 This will yield the same result as if you use multiple filters for the "Heading 1". 这将产生与将多个过滤器用于“标题1”相同的结果。 If you don't like the extra columns, you can also just hide them away. 如果您不喜欢多余的列,也可以将它们隐藏起来。

Step 5: If you now apply this to VBA, the code could look like this: 步骤5:如果现在将其应用于VBA,则代码可能如下所示:

Dim pvt As PivotTable
Set pvt = ActiveSheet.PivotTables("PivotTable1")

ActiveSheet.PivotTables("PivotTable1").AllowMultipleFilters = True

With pvt.PivotFields("Heading 1")
    .ClearAllFilters
    .PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Heading 2"), Value1:=2
End With
With pvt.PivotFields("Help 1")
    .ClearAllFilters
    .PivotFilters.Add2 Type:=xlValueIsSmallerThan, DataField:=ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Heading 2"), Value1:=11
End With
With pvt.PivotFields("Help 2")
    .ClearAllFilters
    .PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Heading 3"), Value1:=4
End With

I hope this is useful. 我希望这是有用的。

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

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