简体   繁体   English

基于字段层次结构的VBA数据透视表格式

[英]VBA pivot table formatting based on field hierarchy

I was tasked at work to write a macro that copies and formats a preliminary pivot table to fit the company wide branding style. 我的工作是编写一个宏,该宏将复制并格式化初步的数据透视表以适合公司范围内的品牌风格。
The basic macro is complete but I am having trouble automating the formatting of the pivot fields based on their hierarchy and dependency. 基本宏已经完成,但是我无法根据它们的层次结构和依存关系自动设置枢轴字段的格式。

The current code looks like this 当前代码如下所示

Sub FormatHierarchy()
    'formatting Hierarchy level 1
    ActiveSheet.PivotTables(1).PivotSelect "'EBITDA category'[All]", _
    xlLabelOnly + xlFirstRow, True

    With Selection
        With .Font
            .Name = "Arial Narrow"
            .Size = 10
            .Bold = True
        End With

        .VerticalAlignment = xlTop
        .HorizontalAlignment = xlLeft
        .WrapText = True
    End With


'formatting Hierarchy level 2
ActiveSheet.PivotTables(1).PivotSelect "Account[All]", _
    xlLabelOnly + xlFirstRow, True

    With Selection
        With .Font
            .Name = "Arial Narrow"
            .Size = 10
            .Bold = False
        End With

        .VerticalAlignment = xlTop
        .HorizontalAlignment = xlLeft
        .WrapText = True
        .IndentLevel = 0
    End With

'formatting Hierarchy level 3
ActiveSheet.PivotTables(1).PivotSelect "SuSa account[All]", _
    xlLabelOnly + xlFirstRow, True

    With Selection
        With .Font
            .Name = "Arial Narrow"
            .Size = 10
            .Bold = False
        End With

        .VerticalAlignment = xlTop
        .HorizontalAlignment = xlLeft
        .WrapText = True
        .IndentLevel = 1
    End With

End Sub

"EBITDA category", "Account" and "SuSa account" will change based on the raw data and whatever the manager decides to call them, so I cannot directly use the names. “ EBITDA类别”,“帐户”和“ SuSa帐户”将根据原始数据以及经理决定调用它们的方式进行更改,因此我无法直接使用这些名称。 Is there a way to directly reference the field names based on their hierarchy? 有没有一种方法可以根据其层次结构直接引用字段名称?

original pivot table 原始数据透视表
resulting pivot table (bold is level 1, normal is level 2 and indented is level 3) 结果数据透视表 (粗体为1级,普通为2级,缩进为3级)

Any help is appreciated. 任何帮助表示赞赏。
Thanks! 谢谢!

I haven't been 100% successful in recreating your setup, but if I understand it correctly, then the following should help: 我没有100%成功地重新创建您的设置,但是如果我正确理解它,那么以下内容应该会有所帮助:

  1. Write function that cycles through each PivotFields (similar to PivotSelect, but using Index instead of name as reference). 编写遍历每个PivotFields的函数(类似于PivotSelect,但是使用Index而不是name作为引用)。
  2. Use "Position" property of PivotField to identify level in hierarchy 使用PivotField的“位置”属性标识层次结构中的级别
  3. Use IF-statement for formatting based on level identified above 使用IF语句根据上面确定的级别进行格式化

Example code: 示例代码:

Dim i as Long
For i = 1 to ActiveSheet.PivotTables(1).PivotFields.Count
If ActiveSheet.PivotTables(1).PivotFields(i).Position = 1 Then
    'Enter your formatting for hirearchy level 1 here
ElseIf ActiveSheet.PivotTables(1).PivotFields(i).Position = 2
    'Enter your formatting for hirearchy level 2 here
ElseIf ActiveSheet.PivotTables(1).PivotFields(i).Position = 3
    'Enter your formatting for hirearchy level 3 here
End If
Next i

Try the code below, explanation inside the code as comments: 尝试下面的代码,并在代码中以注释的形式进行解释:

Option Explicit    

Sub FormatHierarchy()

Dim PvtTbl As PivotTable
Dim PvtFld As PivotField

' set the PivotTable object
Set PvtTbl = ActiveSheet.PivotTables(1)

' loop through all Pivot Fields in Pivot Table
For Each PvtFld In PvtTbl.PivotFields
    Select Case PvtFld.Position
        Case 1
           ' do your format here ...

        Case 2
            ' do your format here ...

        Case 3
            ' do your format here ...

    End Select    
Next PvtFld

End Sub

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

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