[英]Pivot table filtering based on a combo box
I would need your help in coding as I cannot figure out how to achieve it. 我在编码方面需要您的帮助,因为我无法弄清楚如何实现它。 I wanted to link a combo box to influence the filtering of a pivot table but the coding I found cannot address what I want to achieve - ie 我想链接一个组合框以影响数据透视表的筛选,但是我发现的编码无法满足我想要实现的目标,即
1) How could I link the pivot and combo box when they are on two different worksheet? 1)当枢轴和组合框位于两个不同的工作表上时,如何链接它们? - the combo box is in Worksheet1 and the pivot is in Worksheet2. -组合框位于Worksheet1中,而枢轴位于Worksheet2中。
2) How can I have the pivot to refresh the filter to "all" when the combo box (based on geographies) equivalent is "WW"? 2)当组合框(基于地理位置)等效为“ WW”时,如何才能将过滤器刷新为“全部”?
The below code I found is a starting base but ended up with two errors. 我发现以下代码是一个开始的基础,但最终出现两个错误。
With ActiveSheet.Shapes(Application.Caller).ControlFormat
ActiveSheet.PivotTables("Pivot_table1").PivotTableWizard SourceType:=xlDatabase, SourceData:=
.List (.Value)
End With
End Sub
the Errors are the following: a)syntax error from the first line (I linked the macro to the combo box) and b) a compile error expected:expression on the activesheet. 错误如下:a)第一行的语法错误(我将宏链接到组合框),b)预期的编译错误:活动表上的表达式。
Appreciate any hints on how to make it work - thank you very much. 感谢有关如何使其工作的任何提示-非常感谢。
I find an alternative solutions that worked out at the end - thanks all for watching and supporting. 我找到了最终可以解决的替代解决方案-感谢大家的关注和支持。
Sub changeFilters()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim wsChart As Worksheet
Dim wsPivot As Worksheet
Dim SelGeo As Variant
'turn on automatic update / calculation in the Pivot Table
Set wsChart = ThisWorkbook.Sheets("Graph Data")
Set wsPivot = ThisWorkbook.Sheets("PCW_pivot")
Set pt = ThisWorkbook.Sheets("PCW_pivot").PivotTables("Pivot_table1")
Set SelGeo = ThisWorkbook.Sheets("Graph Data").Range("SelGeo")
pt.ManualUpdate = True
Application.ScreenUpdating = False
ThisWorkbook.Sheets("PCW_pivot").PivotTables("Pivot_table1").ClearAllFilters
If ThisWorkbook.Sheets("Graph Data").Range("SelGeo") = WW Then
Exit Sub
End If
For Each pi In pt.PivotFields("Geo").PivotItems
Select Case pi.Name
Case [SelGeo]
pi.Visible = True
Case Else
pi.Visible = False
End Select
Next pi
'turn on automatic update / calculation in the Pivot Table
pt.ManualUpdate = False
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.