通过单元格内部设置图表系列颜色

时间:2018-07-27 14:46:26

标签: excel excel-vba colors

我想知道如何通过引用的单元格内部来更改序列中的每个点颜色。我快到了...

首先,这是一个11维系列,我创建了一个数组来存储颜色:

Dim VectorC(0 To 10) As Long
For x = 0 To 10:
    VectorC(x) = SH1.Cells(12 + x, 3).Interior.Color
Next x

到目前为止,太好了。但是,当我尝试这样做时,请设置颜色系列CH0101

CH0101.Points(1).Format.Fill.ForeColor.Color = VectorC(0)

我收到一个错误消息(因为Color类中缺少CH0101.Points(1).Format.Fill.ForeColor)。

我知道如何使用RGB属性更改系列颜色,但是要做到这一点,我必须使用RGB array存储颜色,但我知道不能这样做。

有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:1)

是否想为此添加工作模型?我喜欢玩它。

enter image description here

Option Explicit

Sub main()
    Dim ws As Worksheet
        Set ws = Sheets("Sheet1")

    Dim rngOfColors As Range
        Set rngOfColors = ws.Range("A5:A9")

    Dim col As Collection
        Set col = New Collection

    Dim cell As Range
    Dim i As Integer
    i = 0
    For Each cell In rngOfColors
        col.Add cell.Interior.Color, CStr(i)
        i = i + 1
    Next cell

    Dim chartObject As chartObject
        Set chartObject = ws.ChartObjects(1)

    Dim myChart As chart
        Set myChart = chartObject.chart

    Dim mySeriesCol As SeriesCollection
        Set mySeriesCol = myChart.SeriesCollection

    Dim mySeries As Series

    i = 0
    For Each mySeries In mySeriesCol
       Dim myPoint As point
        For Each myPoint In mySeries.Points
            myPoint.Format.Fill.ForeColor.RGB = col(CStr(i))
        Next myPoint
            i = i + 1
    Next mySeries
End Sub

答案 1 :(得分:0)

由于@BigBen,我使用以下代码更新了代码:

Dim VectorC(0 To 10, 1 To 3) As Long
For x = 0 To 10:
    VectorC(x, 1) = SH1.Cells(12 + x, 2).Interior.Color Mod 256
    VectorC(x, 2) = (SH1.Cells(12 + x, 2).Interior.Color \ 256) Mod 256
    VectorC(x, 3) = SH1.Cells(12 + x, 2).Interior.Color \ 65536
Next x

CH0101.Points(1).Format.Fill.ForeColor.RGB = RGB(VectorC(0, 1), VectorC(0, 2), VectorC(0, 3))
CH0101.Points(2).Format.Fill.ForeColor.RGB = RGB(VectorC(1, 1), VectorC(1, 2), VectorC(1, 3))...

它奏效了!今天学到了一些东西...:)