【问题标题】:Change color of axis bars in an Excel pivotchart更改 Excel 数据透视图中轴条的颜色
【发布时间】:2018-03-19 01:10:08
【问题描述】:

我在 Excel 2016 中有这个数据透视图

如您所见,轴字段中有两个属性:“日期”和“类别”。

“类别”有两个可能的值:ASC 和 SBT。

目前,与任一值相关的条具有相同的颜色(红色和蓝色)。

如果“类别”是 SBT,我希望条形的颜色必须不同(例如,黄色和绿色)。我怎样才能做到这一点?

谢谢

【问题讨论】:

    标签: excel pivot-table pivot-chart vba


    【解决方案1】:

    试试这个。

    Sub test()
        Dim obj As ChartObject
        Dim cht As Chart
        Dim pnt As Point
        Dim Ws As Worksheet
        Dim s As String
    
        Set Ws = ActiveSheet
        Set obj = Ws.ChartObjects(1)
        Set cht = obj.Chart
    
        With cht
            .ApplyDataLabels
            For Each pnt In .SeriesCollection(1).Points
                With pnt.DataLabel
                    .ShowCategoryName = True
                    .ShowValue = False
                End With
                s = pnt.DataLabel.Text
                If InStr(s, "SBT") Then
                   pnt.Format.Fill.ForeColor.RGB = RGB(255, 2255, 0)
                End If
                 With pnt.DataLabel
                    .ShowCategoryName = False
                End With
            Next pnt
            For Each pnt In .SeriesCollection(2).Points
                With pnt.DataLabel
                    .ShowCategoryName = True
                    .ShowValue = False
                End With
                s = pnt.DataLabel.Text
                If InStr(s, "SBT") Then
                   pnt.Format.Fill.ForeColor.RGB = RGB(29, 219, 22)
                End If
                 With pnt.DataLabel
                    .ShowCategoryName = False
                End With
            Next pnt
        End With
    End Sub
    

    【讨论】:

    • 可能会很好!您知道是否有办法更改图例以将其他颜色添加到类别中?
    • @Ponzaro,legnend 颜色是根据 seriescollection 的颜色设置的。
    • 所以,如果要更改图例颜色,请更改 collection 的颜色。 .SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多