0

有人能告诉我一种简单直接的方法,将工作表中单元格的颜色读入 VBA,然后使用这个 RGB 代码或用什么来为饼图的一部分着色吗?

我正在努力

    Function rgb_color(cl As Range) As String
Dim rgbc As Long, rc As Long, gc As Long, bc As Long
If cl.Cells.Count = 1 Then
rc = cl.Interior.Color Mod 256
rgbc = Int(cl.Interior.Color / 256)
gc = rgbc Mod 256
bc = Int(rgbc / 256)
Else
End If
End Function

Sub ColorScheme(cht As Chart, i As Long)

    Dim Colors

    Select Case i Mod 10
        Case 0
        rgb_color (C2)
        rgb_color (C3)
        rgb_color (C4)

所以第一部分是从单元格中获取 RGB 代码的函数,我有各种要着色的图表,这就是我选择不同 case 语句的原因。我知道如果需要我可以手动设置 RGB 颜色,但是有没有一种简单的方法可以将三个单元格(C2 到 C4)中的每一个的不同 rc、bc、gc 变量用作 RGB 值?它看起来像一个数组

RGB(rc of C2; gc of C2; bc of C2)
same for C3 and C4

那么下一个案例用同样的程序呢?

''############################################### ###### 编辑蒂姆的评论

如果我得到蒂姆(下面的评论)正确,我可以写

Sub ColorScheme(cht As Chart, i As Long)

        Dim Colors

        Select Case i Mod 10
        Case 0 
ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).Points(x).Format.Fill.For‌eColor.RGB = Range("A1").Interior.Color

如果那是错误的,有人可能会纠正我吗?

4

1 回答 1

0

这准确地显示了如何去做(或者至少我会如何去做)。让它发挥作用取决于你。

Sub Tester()
Dim cht As Chart, x As Long
    Set cht = ActiveSheet.ChartObjects(1).Chart

    For x = 0 To 1
        'set pie chart source data
        SetColorScheme cht, x 'this will apply the color scheme
        cht.CopyPicture
        '...paste to point in other chart
    Next x

End Sub

'set the color scheme on the passed chart object
Sub SetColorScheme(cht As Chart, i As Long)

    Dim y_off As Long, rngColors As Range
    Dim x As Long

    y_off = i Mod 10
    'this is the range of cells which has the colors you want to apply
    Set rngColors = ThisWorkbook.Sheets("colors").Range("A1:C1").Offset(y_off, 0)

    With cht.SeriesCollection(1)
        'loop though the points and apply the corresponding fill color from the cell
        For x = 1 To .Points.Count
            .Points(x).Format.Fill.ForeColor.RGB = _
                             rngColors.Cells(x).Interior.Color
        Next x
    End With

End Sub
于 2013-07-02T16:46:36.317 回答