简体   繁体   中英

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. 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)

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:

  1. Write function that cycles through each PivotFields (similar to PivotSelect, but using Index instead of name as reference).
  2. Use "Position" property of PivotField to identify level in hierarchy
  3. Use IF-statement for formatting based on level identified above

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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