【问题标题】:Set chart series color by cells interior按单元格内部设置图表系列颜色
【发布时间】:2019-01-04 17:23:51
【问题描述】:

我想知道如何通过它所指的单元格内部来更改系列上的每个点颜色。我快到了……

首先,它是一个 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)

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

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

有什么想法吗??谢谢。

【问题讨论】:

标签: excel vba colors


【解决方案1】:

想为此添加一个工作模型吗?因为我喜欢玩它。

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

【讨论】:

    【解决方案2】:

    感谢@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))...
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 2014-05-20
      • 1970-01-01
      • 2018-04-24
      • 2021-09-23
      • 2017-01-20
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多