Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA用于在多值筛选器上筛选数据透视表-是否可能?_Vba_Excel_Filter_Pivot Table - Fatal编程技术网

VBA用于在多值筛选器上筛选数据透视表-是否可能?

VBA用于在多值筛选器上筛选数据透视表-是否可能?,vba,excel,filter,pivot-table,Vba,Excel,Filter,Pivot Table,我有一个pivot表,我创建了一个UserForm,它应该允许用户选择某些过滤选项。我想根据链接到列的各种条件筛选行,具体取决于用户输入。然而,我似乎无法让它在多值过滤器上过滤行。这似乎也不可能从前端实现,但我仍然希望VBA能找到一种方法 以下是相关代码(在UserForm的OK按钮后面)的启动方式: 'ok button - this is the big one, telling the button what to do with the selected options! Privat

我有一个pivot表,我创建了一个UserForm,它应该允许用户选择某些过滤选项。我想根据链接到列的各种条件筛选行,具体取决于用户输入。然而,我似乎无法让它在多值过滤器上过滤行。这似乎也不可能从前端实现,但我仍然希望VBA能找到一种方法

以下是相关代码(在UserForm的OK按钮后面)的启动方式:

'ok button - this is the big one, telling the button what to do with the selected options!

Private Sub CommandButton1_Click()

'targeting our familiar pivot table

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

'allowing multiple filters to be used at once
pvt.AllowMultipleFilters = True

'and identifying the row that we want to filter - n.b. this would need to change if corporate wanted to use another field as their lowest level row.
Dim pvtFieldBrand As PivotField
Set pvtFieldBrand = pvt.PivotFields("final brand")

'clearing any filters already set
pvtFieldBrand.ClearAllFilters  
然后是过滤器。
这一个单独工作:

'Filtering depending on whether 1+, 2+ or all 3 audiences favour it

Dim pvtFieldNoAud As PivotField 'then hide the column - at the top of this script, in fact, just in case it's not already hidden
Set pvtFieldNoAud = pvt.PivotFields("Sum of # Audiences")


If OptionButton3 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsBetween, DataField:=pvtFieldNoAud, Value1:=1, Value2:=3

End If

If OptionButton4 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsBetween, DataField:=pvtFieldNoAud, Value1:=2, Value2:=3

End If

If OptionButton5 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueEquals, DataField:=pvtFieldNoAud, Value1:=3

End If
然后是这些过滤器,我打算将其设置为多选选项,但它们只能在完全隔离的情况下工作-不能相互组合,也不能与任何其他过滤器组合:

'Only show if favoured by the selected audiences

Dim pvtFieldWW As PivotField
Set pvtFieldWW = pvt.PivotFields("Sum of WW overindex?")

Dim pvtFieldUU As PivotField
Set pvtFieldUU = pvt.PivotFields("Sum of UU overindex?")

Dim pvtFieldRR As PivotField
Set pvtFieldRR = pvt.PivotFields("Sum of RR overindex?")


If CheckBox1 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldWW, Value1:=0
End If

If CheckBox2 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldUU, Value1:=0
End If

If CheckBox3 = True Then
pvtFieldBrand.PivotFilters.Add2 Type:=xlValueIsGreaterThan, DataField:=pvtFieldRR, Value1:=0
End If
等等


我需要用户能够以多达6种方式筛选此数据透视表。这可能是通过前端的切片器和报表过滤器来完成的,但我想限制用户的选项和用户错误的可能性,因此我计划将所有相关选项保留在UserForm中,然后锁定一些功能,这样他们就不能对透视表进行任何其他更改。任何想法都非常感谢

有一个简单的解决方法,允许您在数据透视表上使用多个筛选器。这里是一个逐步的方法,您可以将其用于您的方法

步骤1:向数据源中添加具有新标题和每行中任何常量值的辅助列。(每个额外筛选器需要一个helper列。如果要使用3个筛选器,则需要两个helper列)

步骤2:将Helpercolumn属性添加到透视表的行字段中。

步骤3:选择所有行属性都位于一行中的表格布局。

步骤4:现在您可以应用不同的过滤器,行字段中的每个属性都有一个过滤器。这将产生与对“标题1”使用多个过滤器相同的结果

步骤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

哦-我得到的错误消息是“运行时错误1004-应用程序定义或对象定义错误”。它位于代码中表示调用的第二个筛选器的任何一行。即使在VBA中,也不能对同一字段应用多个值筛选器。AllowMultipleFilters设置允许您对同一字段应用多个类型的筛选器,但不能应用多个相同类型的筛选器。谢谢您的确认。如果有人对阅读感兴趣,我想我下一步将尝试使用VBA过滤源数据,然后将可见单元格复制并粘贴到新选项卡,然后从该选项卡创建透视表(如这里所建议的(不是我的问题):)。以前没有这样做过,但我会录制一些宏,看看是否可以将它们缝合在一起。很高兴报告,我上面评论中的方法非常有效!:-)