soa*_*dos 5 excel vba pivot-table excel-vba
我有一个数据透视表结构,看起来像(例如,数据透视表UI的“ ROWS”框中有三个条目)
我知道我可以做(在VBA)获取所有类别,子类别和子类别分PT.PivotFields(3).PivotItems()
,PT.PivotFields(2).PivotItems()
并PT.PivotFields(1).PivotItems()
分别,其中PT是我的数据透视表。
如何找出每个类别中的哪些子类别,以及类别中的子子类别相同的子类别?
我尝试使用,PT.PivotFields(3).PivotItems()(1).ChildItems()
但是尝试时出现错误<Unable to get the ChildItems property of the PivotItem class>
和相同的内容ParentItem
。
知道我该怎么做吗?
我正在寻找的例子。拿下面的数据透视表,并枚举(以某种方式):
要求: \n构建一个表,显示给定 的Items
所有RowFields
和的所有组合。\nColumnsFields
PivotTable
解决方案: \n这可以通过设置数据透视表以及行、列和数据字段的一些属性和方法来实现,如下所示:
\n\n设置这些数据透视表属性:
\nRowGrand、ColumnGrand、MergeLabels、RowAxisLayout
为 ColumnFields 设置以下属性:
\n方向
为 RowField 设置以下属性:
\nRepeatLabels、Subtotals
为数据字段设置以下属性:
\n方向
\n
程序:
\n\nSub PivotTable_Hierarchy_Rows_And_Columns(pt As PivotTable, _ \n aPtHierarchy As Variant, blClearFilters As Boolean, blIncludeCols As Boolean)\n
Run Code Online (Sandbox Code Playgroud)\n\n此过程调整所有上述属性,以便在\xe2\x80\x9ctable 中显示数据透视表,例如 \xe2\x80\x9d格式显示数据透视表,生成具有数据透视表\xe2\x80\x99s 层次结构的数组。它还提供了用于清除或不清除数据透视表筛选器以及在层次结构中包含或不包含 ColumnField 的选项。
\n\n参数:
\n Pt:目标数据透视表
\n aPtHierarchy:包含目标数据透视表层次结构的数组输出。
\n blClearFilters:布尔值。确定是否清除所有数据透视表筛选器。\n blIncludeCols : 布尔值。用于在输出层次结构中包含或不包含 ColumnField。
语法:
\n Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, blClearFilters, blIncludeCols)
\n
编程语言:
\n\nSub PivotTable_Hierarchy_Rows_And_Columns(pt As PivotTable, _\n aPtHierarchy As Variant, blClearFilters As Boolean, blIncludeCols As Boolean)\nDim pf As PivotField\n\n Rem PivotTable Properties & Methods\n With pt\n .RowGrand = False\n .ColumnGrand = False\n .MergeLabels = False\n .RowAxisLayout xlTabularRow\n If blClearFilters Then .ClearAllFilters\n End With\n\n Rem ColumnFields Properties\n For Each pf In pt.ColumnFields\n If blIncludeCols Then\n pf.Orientation = xlRowField\n Else\n pf.Orientation = xlHidden\n End If: Next\n\n Rem RowFields Properties\n For Each pf In pt.RowFields\n With pf\n On Error Resume Next\n .RepeatLabels = True\n .Subtotals = Array(False, False, False, False, _\n False, False, False, False, False, False, False, False)\n On Error GoTo 0\n End With: Next\n\n Rem DataFields Properties\n For Each pf In pt.DataFields\n pf.Orientation = xlHidden\n Next\n\n Rem Set Hierarchy Array\n aPtHierarchy = pt.RowRange.Value2\n\n End Sub\n
Run Code Online (Sandbox Code Playgroud)\n\n
例子:
\n\n假设我们需要获取图 1 中数据透视表的层次结构。1.\n请注意,数据透视表应用了一些筛选器。
\n\nPivotTable_Hierarchy_Rows_And_Columns
根据所需的结果,可以按如下方式调用该过程:
Sub PivotTable_Hierarchy_Rows_And_Columns_TEST()\nDim pt As PivotTable, aPtHierarchy As Variant\n\n \'Set PivotTable - Change worksheet and pivottable name as required\n Set pt = ThisWorkbook.Worksheets("Summary").PivotTables("PtTst") \n\n \'1. To obtain the Hierarchy for Rows and Columns, clearing all the filters applied to the PivotTable try this: \n Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, True, True) \'See results in Fig. R1 (Table & Array) \n\n \xe2\x80\x982. To obtain the Hierarchy for Rows only, clearing all the filters applied to the PivotTable try this: \n Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, True, False) \'See results in Fig. R2 (Table & Array) \n\n \'3. To obtain the Hierarchy for Rows and Columns with the filters currently applied to the PivotTable try this: \n Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, False, True) \'See results in Fig. R3 (Table & Array) \n\n \'4. To obtain the Hierarchy for Rows only with the filters currently applied to the PivotTable try this: \n Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, False, False) \'See results in Fig. R4 (Table & Array) \n\n End Sub\n
Run Code Online (Sandbox Code Playgroud)\n\n有关所用资源的更多信息,请参阅以下页面:
\n\n数据透视表对象 (Excel)
\n PivotTable.RowAxisLayout 方法 (Excel)
\n PivotField 对象 (Excel)
归档时间: |
|
查看次数: |
1169 次 |
最近记录: |