[英]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错误:
Report Filter
. Report Filter
器”的任何字段。 You cannot use the CurrentPage
to filter any rows or columns CurrentPage
来过滤任何行或列 On the second point, this is where the call to Range
might be relevant. 在第二点上,这是对
Range
的调用可能相关的地方。
.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: 要解决这些问题,有两种选择:
PivotItems
and set Visible = True/False
PivotItems
并设置Visible = True/False
CurrentPage
, you can iterate the PivotItems
for that PivotField
and check that one matches. CurrentPage
存在的值,则可以迭代该PivotField
的PivotItems
并检查是否匹配。 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.