枚举子组pivottable vba

时间:2017-11-16 00:07:51

标签: vba excel-vba pivot-table excel

我有一个看起来像的数据透视表结构(即可在枢轴表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时也是如此。

知道我该怎么做吗?

我正在寻找的一个例子。使用下面的数据透视表,并枚举(以某种方式):

a有子类别d,e; b有子类别e,f; c有子类别d,e,f;如果列位置有多个级别,它将是相同的。 enter image description here

3 个答案:

答案 0 :(得分:2)

  • <强>要求: 构建一个表格,显示给定Items的所有RowFieldsColumnsFields的所有PivotTable组合。

  • <强>解决方案: 这可以通过设置数据透视表和行,列和数据字段的一些属性和方法来实现,如下所示:

    1. 设置这些数据透视表属性:
      RowGrand,ColumnGrand,MergeLabels,RowAxisLayout

    2. 为ColumnFields设置这些属性:
      方向

    3. 为RowField设置以下属性:
      RepeatLabels,Subtotals

    4. 为DataField设置以下属性:
      定向

  • 程序:

    Sub PivotTable_Hierarchy_Rows_And_Columns(pt As PivotTable, _   
        aPtHierarchy As Variant, blClearFilters As Boolean, blIncludeCols As Boolean)
    

    此过程调整所有上述属性,以便以“表格”格式显示数据透视表,生成具有数据透视表层次结构的数组。它还提供了清除或不清除数据透视表过滤器以及是否包含层次结构中的ColumnField的选项。

    参数:
    Pt :目标数据透视表
    aPtHierarchy :包含目标数据透视表层次结构的数组输出 blClearFilters :布尔值。确定是否清除所有数据透视表筛选器。 blIncludeCols :布尔值。用于在输出层次结构中包含或不包含ColumnField。

    语法:
    Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, blClearFilters, blIncludeCols)

  • <强> VBA:

    Sub PivotTable_Hierarchy_Rows_And_Columns(pt As PivotTable, _
        aPtHierarchy As Variant, blClearFilters As Boolean, blIncludeCols As Boolean)
    Dim pf As PivotField
    
        Rem PivotTable Properties & Methods
        With pt
            .RowGrand = False
            .ColumnGrand = False
            .MergeLabels = False
            .RowAxisLayout xlTabularRow
            If blClearFilters Then .ClearAllFilters
        End With
    
        Rem ColumnFields Properties
        For Each pf In pt.ColumnFields
            If blIncludeCols Then
                pf.Orientation = xlRowField
            Else
                pf.Orientation = xlHidden
        End If: Next
    
        Rem RowFields Properties
        For Each pf In pt.RowFields
            With pf
                On Error Resume Next
                .RepeatLabels = True
                .Subtotals = Array(False, False, False, False, _
                    False, False, False, False, False, False, False, False)
                On Error GoTo 0
        End With: Next
    
        Rem DataFields Properties
        For Each pf In pt.DataFields
            pf.Orientation = xlHidden
        Next
    
        Rem Set Hierarchy Array
        aPtHierarchy = pt.RowRange.Value2
    
        End Sub
    


  • 示例:

    假设我们需要获取图2中的数据透视表的层次结构。 1。 请注意,数据透视表已应用了一些过滤器。

    根据所需的结果,可以按如下方式调用过程PivotTable_Hierarchy_Rows_And_Columns

    Sub PivotTable_Hierarchy_Rows_And_Columns_TEST()
    Dim pt As PivotTable, aPtHierarchy As Variant
    
        'Set PivotTable - Change worksheet and pivottable name as required
        Set pt = ThisWorkbook.Worksheets("Summary").PivotTables("PtTst")      
    
        '1. To obtain the Hierarchy for Rows and Columns, clearing all the filters applied to the PivotTable try this:  
        Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, True, True)    'See results in Fig. R1 (Table & Array)  
    
        ‘2. To obtain the Hierarchy for Rows only, clearing all the filters applied to the PivotTable try this:  
        Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, True, False)    'See results in Fig. R2   (Table & Array)  
    
        '3. To obtain the Hierarchy for Rows and Columns with the filters currently applied to the PivotTable try this:  
        Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, False, True)    'See results in Fig. R3   (Table & Array)  
    
        '4. To obtain the Hierarchy for Rows only with the filters currently applied to the PivotTable try this:  
        Call PivotTable_Hierarchy_Rows_And_Columns(pt, aPtHierarchy, False, False)    'See results in Fig. R4   (Table & Array)  
    
        End Sub
    


    图1 Fig. 1


    图R1 Fig. R1


    图R2 Fig. R2


    图R3 Fig. R3


    图R4 Fig. R4

有关所用资源的其他信息,请参阅以下页面:

PivotTable Object (Excel)
PivotTable.RowAxisLayout Method (Excel)
PivotField Object (Excel)

答案 1 :(得分:0)

我不知道您有多少类别和子类别,但您可以将所有内容放入多个数组(矩阵),然后按照您的需要进行管理。

试试这个:

Sub GetCategories_SubCat()

Dim PTable As PivotTable
Dim matrix() As Variant
Dim matrix_s() As Variant
Dim msg$

Set PTable = ActiveSheet.PivotTables("PT")

ReDim matrix(1 To PTable.RowFields.Count)

For i = 1 To PTable.RowFields.Count
    matrix(i) = PTable.RowFields(i)
    For j = 1 To PTable.RowFields(i).PivotItems.Count
        ReDim matrix_s(1 To PTable.RowFields.Count, 1 To PTable.RowFields(i).PivotItems.Count)
        matrix_s(i, j) = PTable.RowFields(i).PivotItems(j)
    Next j
Next i

End Sub

答案 2 :(得分:0)

我认为你可以使用GetPivotData函数来尝试获取每组枢轴字段的值。如果没有抛出错误,您就知道某个类别具有某个子和子子类别。有关三个级别的表格,请参阅我的粗略代码:

Sub ExaminePT()
Dim pt As PivotTable
Dim pf As PivotField

Dim Lev1 As PivotField
Dim Lev2 As PivotField
Dim lev3 As PivotField

Dim pi1 As PivotItem
Dim pi2 As PivotItem
Dim pi3 As PivotItem

Dim checkVal As Double

Set pt = ActiveSheet.PivotTables(1)
For Each pf In pt.PivotFields
    If pf.Orientation = xlRowField Then
        Select Case pf.Position
            Case Is = 1
                Set Lev1 = pf
            Case Is = 2
                Set Lev2 = pf
            Case Is = 3
                Set lev3 = pf
        End Select
    End If
Next pf

For Each pi1 In Lev1.PivotItems
    For Each pi2 In Lev2.PivotItems
        For Each pi3 In lev3.PivotItems
            On Error Resume Next
            checkVal = pt.GetPivotData("Incremental Benefits 2017", Lev1.Name, pi1.Name, _
                Lev2.Name, pi2.Name, lev3.Name, pi3.Name)
            If Err.Number = 0 Then Debug.Print pi1 & "| " & pi2 & "| " & pi3
            On Error GoTo 0
        Next pi3
    Next pi2
Next pi1

End Sub

在第一个循环中,我将类别,子子类和子子类分配给变量,然后执行上面建议的操作。结果写在立即窗口中,这是解决方案的草图。

可能只需要很少的努力就可以将它变成更通用的程序来检查任意数量的嵌套级别的数据透视表。