0

我正在寻找一种基于水平类别轴的值来格式化饼图中切片颜色的方法。

例如,我有一个带有以下数据标签的饼图(来自类别轴标签):

  • 苹果
  • 香蕉
  • 橘子
  • 葡萄

每个水果的总数加起来为 100%。

例如,在饼图中,代表 Apples 的切片始终为绿色。香蕉总是黄色的,等等。每个月我都必须更新饼图,每个月苹果、香蕉、橙子和葡萄的值都会发生变化。但是,切片颜色不会改变。

我的问题是 - 有没有办法每月更新饼图并保持与每种水果相关的颜色?还要记住,有些月份可能会有额外的水果,有些月份可能会省略一些水果。

我试过根据每个切片的Points(x)值来改变它,但是因为系列的数量可能每个月都在变化,所以它不起作用。正如您从下面看到的那样,我已经设法根据每个切片的Points(x)值对其进行更改,但我想知道是否可以包含一个 IF 循环来表达类似...IF category axis = "Apples", Selection.Format.Fill ... (fill slice with RGB(X, X, X))等的内容。

Sub test()

    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 144, 44)
        .Transparency = 0
        .Solid
    End With
    ActiveChart.SeriesCollection(1).Points(3).Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(112, 48, 30)
        .Transparency = 0
        .Solid
    End With
        ActiveChart.SeriesCollection(1).Points(6).Select
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(112, 48, 160)
        .Transparency = 0
        .Solid
    End With
End Sub

如果有人可以提供帮助,我将不胜感激。

4

1 回答 1

0

像这样的东西应该工作

Sub Tester()

    Dim cht As Chart, pts As Points
    Dim sc As Series
    Dim x As Integer
    Dim sliceName As String, clr As Long

    Set cht = ActiveSheet.ChartObjects(1).Chart
    Set sc = cht.SeriesCollection(1)
    Set pts = sc.Points

    'loop through all the points
    For x = 1 To pts.Count

        sliceName = sc.XValues(x)

        Select Case sliceName
            'assign a specific color...
            Case "A": clr = vbYellow 'RGB(255,255,0)
            Case "B": clr = vbGreen  'RGB(0,255,0)
            Case "C": clr = vbRed    'RGB(255,0,0)
            Case Else: clr = -1 '...or do nothing
        End Select

        If clr <> -1 Then
            With pts(x).Format.Fill
                .ForeColor.RGB = clr
                .Transparency = 0
                .Solid
            End With
        End If

    Next x

End Sub
于 2013-07-10T04:22:35.323 回答