代码之家  ›  专栏  ›  技术社区  ›  Pspl

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

  •  0
  • Pspl  · 技术社区  · 7 年前

    我想知道我如何能改变一个系列的每一个点的颜色由它所指的细胞内部。我就快到了。。。

    首先,这是一个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 回复  |  直到 7 年前
        1
  •  1
  •   learnAsWeGo    7 年前

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

    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
    
        2
  •  0
  •   Pspl    7 年前

    多亏了@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))...
    

    而且成功了!今天学到了一些东西。。。:)

    推荐文章