简体   繁体   English

组合框更改Excel数据透视表上的过滤器

[英]combo box to change filters on excel pivot table

Excel 2013. I am using 3 combo boxes to change filters on the pivot table. Excel2013。我正在使用3个组合框来更改数据透视表上的过滤器。 My first combo box has "Project1", "Project2" & All. 我的第一个组合框为“ Project1”,“ Project2”和“全部”。 My second combo box has "Customer1", "Customer2" & All. 我的第二个组合框有“ Customer1”,“ Customer2”和“ All”。 My third combo box has "Country1", "Country2" & All. 我的第三个组合框有“ Country1”,“ Country2”和“ All”。

I am using 9 pivot tables, all of them have filters as [Project], [Customer], [Country]. 我正在使用9个数据透视表,它们都具有[项目],[客户],[国家/地区]过滤器。

My intention is to change first combo box to Project1 & all the pivot tables filter should change as Project1.I am successfully able to do that. 我的意图是将第一个组合框更改为Project1,并且所有数据透视表过滤器都应更改为Project1。我能够成功做到这一点。

However when I select the first combo box as "All". 但是,当我选择第一个组合框为“全部”时。 First Combo box cell link to Y1. 第一个组合框单元格链接到Y1。 I get VBA Run time error 1004: Application-defined or object-defined error. 我收到VBA运行时错误1004:应用程序定义或对象定义的错误。

Sub ProjectName()

ActiveSheet.PivotTables("PVT1").PivotFields("Project Name").ClearAllFilters
ActiveSheet.PivotTables("PVT2").PivotFields("Project Name").ClearAllFilters
ActiveSheet.PivotTables("PVT3").PivotFields("Project Name").ClearAllFilters

    ActiveSheet.PivotTables("PVT1").PivotFields("Project Name").CurrentPage = Range("Y1").Text
    ActiveSheet.PivotTables("PVT2").PivotFields("Project Name").CurrentPage = Range("Y1").Text
    ActiveSheet.PivotTables("PVT3").PivotFields("Project Name").CurrentPage = Range("Y1").Text 

Since the first three lines of code go without issue, I will assume that the Pivot Table PVT1 and the Field Project Name all exist. 由于前三行代码没有问题,因此我假定数据透视表PVT1和字段Project Name都存在。 This places the error somewhere after that. 此后将错误放置在某处。

For the call to .CurrentPage you will get a 1004 error for the following reasons: 对于.CurrentPage的调用,由于以下原因,您将收到1004错误:

  • Using this to try and filter any field that is not set as a Report Filter . 使用它来尝试过滤未设置为Report Filter器”的任何字段。 You cannot use the CurrentPage to filter any rows or columns 您不能使用CurrentPage来过滤任何行或列
  • Setting a value which does not exist in the list of possible values 设置一个可能值列表中不存在的值

On the second point, this is where the call to Range might be relevant. 在第二点上,这是对Range的调用可能相关的地方。

  • Verify that the value there exists in the list of possible ones. 验证该值是否存在于可能的值列表中。
  • Also be aware you are using .Text which will use the display value of the cell and not its underlying .Value 另请注意,您正在使用.Text ,它将使用单元格的显示值,而不是其基础.Value

To resolve these issues, there are a couple of options: 要解决这些问题,有两种选择:

  • For the case where you want to filter data that is on the row or column (and not in the filters section) you can go through PivotItems and set Visible = True/False 对于要过滤行或列上(而不是过滤器部分中)的数据的情况,可以通过PivotItems并设置Visible = True/False
  • You can also set a label filter from VBA if you want that instead of the manual filter 如果需要,也可以从VBA设置标签过滤器,而不是手动过滤器
  • If you want to check for a value existing in the CurrentPage , you can iterate the PivotItems for that PivotField and check that one matches. 如果要检查CurrentPage存在的值,则可以迭代该PivotFieldPivotItems并检查是否匹配。 The code for that is very similar to the For Each loop with the check on value, just don't set Visible . 该代码与带有检查值的For Each循环非常相似,只是不要设置Visible

Code for setting a filter on a row or column 用于在行或列上设置过滤器的代码

Sub FilterPivotField()

    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables("PVT1")

    Dim pf As PivotField
    Set pf = pt.PivotFields("C")

    pf.ClearAllFilters

    'slow iterates all items and sets Visible (manual filter)
    Dim pi As PivotItem
    For Each pi In pf.PivotItems
        pi.Visible = (pi.Name = Range("J2"))
    Next

    'fast way sets a label filter
    pf.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:=Range("J2")

End Sub

Picture of ranges 范围图片

在此处输入图片说明

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

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