简体   繁体   English

将小于筛选器添加到Excel VBA中的数据透视表

[英]Adding a Less Than filter to a pivot table in Excel VBA

I'm trying to add a filter to a column in a pivot table whenever a worksheet is activated in Excel. 每当在Excel中激活工作表时,我都试图将筛选器添加到数据透视表中的列。

I have the following so far: 到目前为止,我有以下内容:

   Dim pvt As PivotTable
   Dim pvtField As PivotField

   Set pvt = ActiveSheet.PivotTables("MyPivotTable")
   Set pvtField = pvt.PivotFields("MyPivotColumnTopField")

   'First refresh the table
   pvt.RefreshTable

   'Clear Out Any Previous Filtering
   pvtField.ClearAllFilters

   'Add the less than filter
   pvtField.PivotFilters.Add xlValueIsLessThan, Value1:="1000"

But I get an Invalid Procedure call or argument error on the line that is trying to add the filter. 但是我在尝试添加过滤器的行上收到Invalid Procedure call or argument错误。

I've also tried: 我也尝试过:

   pvtField.PivotFilters.Add(xlValueIsLessThan, Value1:="1000")

but get a syntax error with that. 但是会收到语法错误。

I'm looking at the documentation at https://msdn.microsoft.com/en-us/vba/excel-vba/articles/pivotfilters-add-method-excel but cannot figure it out. 我正在https://msdn.microsoft.com/zh-cn/vba/excel-vba/articles/pivotfilters-add-method-excel上查看文档,但无法弄清楚。

You're using .Add but you should be using .Add2 as per that link. 您正在使用.Add,但您应该根据该链接使用.Add2。

pf.PivotFilters.Add2 Type:=xlCaptionIsLessThan, Value1:=1000

(I note that link has the correct syntax at the top, but the incorrect syntax at the bottom). (我注意到链接的顶部语法正确,但底部语法错误)。

Normally the easiest way to troubleshoot syntax like this is to fire up the Macro recorder, perform your actions manually, then inspect the code. 通常,解决此类语法最简单的方法是启动宏记录器,手动执行操作,然后检查代码。 In this case, that's problematic. 在这种情况下,这是有问题的。 For instance, if I start with the PivotTable shown below right: 例如,如果我从右下方显示的数据透视表开始:

在此处输入图片说明

...and then clear all filters before setting a Label filter: ...然后清除所有过滤器,然后再设置标签过滤器:

在此处输入图片说明 在此处输入图片说明

...then I get the exact result I want: ...然后我得到想要的确切结果:

在此处输入图片说明

...and I get the following code: ...我得到以下代码:

Sub Macro6()

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Test").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Test").PivotFilters.Add2 _
        Type:=xlCaptionIsLessThan, Value1:="11"
End Sub

But weirdly when I run that code, I get this: 但是奇怪的是,当我运行该代码时,我得到了:

在此处输入图片说明

Despite the fact that the Macro Recorder spits out code with the 11 in a string ie "11", you don't want to feed in a string. 尽管宏录制吐出一个字符串,即“11”与11码的事实,你希望在一个字符串饲料。 Why the Macro Recorder would tell you otherwise is beyond me. 为何宏记录器会告诉您其他原因,这超出了我的范围。

Based on @jeffreyweir's answer (which did a string based filter for me), here is a working example of using .Add2 with both a string based filter and a numerical filter. 基于@jeffreyweir的答案(为我做了一个基于字符串的过滤器),这是一个将.Add2与基于字符串的过滤器和数值过滤器结合使用的工作示例。

This is my example table and pivot table: 这是我的示例表和数据透视表: 示例Excel表

And this is code I have behind each button: 这是我在每个按钮后面的代码:

Sub Button1_Click()
Dim pf As PivotField

    Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("Numbers")
    pf.ClearAllFilters
    pf.PivotFilters.Add2 Type:=xlCaptionIsLessThan, Value1:="11"
End Sub

Sub Button2_Click()
Dim pf As PivotField

    Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("Numbers")
    pf.ClearAllFilters
    pf.PivotFilters.Add2 Type:=xlCaptionIsLessThan, Value1:=11

End Sub

Sub Button3_Click()
Dim pf As PivotField

    Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("Numbers")
    pf.ClearAllFilters
End Sub

Note that: 注意:

  • Button1 is the Show < "11" button which does a string based filter Button1Show < "11"按钮,它执行基于字符串的过滤器
  • Button2 is the Show < 11 button which does a numeric filter Button2Show < 11按钮,它执行数字过滤
  • Button3 is the Show All button which removes the filter Button3是“ Show All按钮,可删除过滤器

When I click the Show < "11" button (ie Button1) I get the following result: 当我单击Show < "11"按钮(即Button1)时,得到以下结果:

单击Button1之后

When I click the Show < 11 button (ie Button2) I get the following (desired) result: 当我单击Show < 11按钮(即Button2)时,得到以下(期望的)结果:

单击Button3之后

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

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