简体   繁体   English

用数据透视表值填充组合框

[英]Populate a Combo Box with Pivot Table Values

I have a combo box with a list of names, that filters a pivot table. 我有一个带有名称列表的组合框,用于过滤数据透视表。
Each name I select changes the values shown at the pivot table. 我选择的每个名称都会更改数据透视表上显示的值。

I need a code that populates a second combo box with those pivot tables values. 我需要一个代码,用这些数据透视表值填充第二个组合框。 When I choose a name on the first combo box, the options at the second one will be the ones filtered by that name on the pivot table. 当我在第一个组合框中选择一个名称时,第二个组合框中的选项将是在透视表上按该名称过滤的选项。

I have done that before in a simpler manner with fixed tables, but now the values are going to change constantly, and that's why I need to use a pivot table for that. 我以前用固定表以更简单的方式完成了此操作,但是现在值将不断变化,这就是为什么我需要为此使用数据透视表的原因。

在此处输入图片说明

You can use the DataRange of your PivotField as source for your ComboBox . 您可以将PivotFieldDataRange用作ComboBox源。

You may either add each cell's value to the ComboBox items ... 您可以将每个单元格的值添加到ComboBox项目中...

Dim aCell As Range
For Each aCell In ActiveSheet.PivotTables(1).RowFields(1).DataRange.Cells
    ActiveSheet.ComboBox1.AddItem aCell.Value
Next aCell

... or the whole ComboBox.List in one by a simple assignment: ...或将整个ComboBox.List通过一个简单的赋值合而为一:

ActiveSheet.ComboBox1.List = ActiveSheet.PivotTables(1).RowFields(1).DataRange.Value

I guess you used your "TIMBRA..."-list as the only row field in your pivot table. 我想您将“ TIMBRA ...”列表用作数据透视表中的唯一行字段。
If not, you can address the DataRange by names instead, eg 如果没有,您可以改用名称来寻址DataRange,例如

ActiveWorkbook.Sheets("...").PivotTables("...").PivotFields("...").DataRange

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

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