0

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

在此处输入图像描述

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

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

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

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

谢谢

4

1 回答 1

0

试试这个。

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
于 2017-10-08T07:27:47.390 回答