尝试使用vba为数据透视表中的列字段标题赋予颜色

问题描述 投票:0回答:1

我正在尝试为数据透视表中各列的字体标题提供白色。

我尝试这样做:

' Format column field headers
For Each pvtField In pvtTable.ColumnFields
    With pvtField.LabelRange
        .Interior.Color = RGB(0, 0, 77) ' Dark Blue
        .Font.Color = RGB(255, 255, 255) ' White
        .Font.Bold = True
    End With
Next pvtField

但是没有用,然后我尝试了更手动的方式,像这样:

' Column headers are the fith row in TableRange2
Set headerRow = pvtTable.TableRange2.Rows(5)
With headerRow
    .Interior.Color = RGB(0, 0, 77) ' Dark Blue
    .Font.Color = RGB(255, 255, 255) ' White
    '.Font.ColorIndex = 2 ' White ' White
    .Font.Bold = True
End With

通过第二个代码,我设法得到 .Interior.Color = RGB(0, 0, 77) ' 深蓝色正确,但字体颜色不是白色。

有什么想法吗?

excel vba
1个回答
0
投票
Sub HighLightPvtHeader()
    Dim pvtTable As PivotTable
    Dim headerRow As Range
    Set pvtTable = Sheet1.PivotTables(1)
    With pvtTable.TableRange1.Rows(2)
        Set headerRow = .Resize(, .Columns.Count - 1).Offset(, 1) ' modify as needed
    End With
    With headerRow
        .Interior.Color = RGB(0, 0, 77) ' Dark Blue
        .Font.Color = vbWhite
        .Font.Bold = True
    End With
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.