VBA更改图表的颜色与ColorIndex

发布于 2025-01-26 21:30:58 字数 1159 浏览 4 评论 0 原文

下面的代码可以使用RGB的三个值设置四行的颜色。三个RGB值以数组( clr )填充,这不是很清楚。使用 hex 数字, colorIndex 或如 vbblack colorIndex colorIndex 是否有其他方法?

Sub CreateChart()
Dim i As Long
Dim j As Long
Dim Clr() As Variant

'Clr = Array("vbBlack", "vbBlue", "vbRed", "vbCyan", "vbGreen", "vbMagenta")    ' colors as types
'Clr = Array("&H0", "&HFF0000", "&Hff", "&HFFFF00", "&HFF00", "&HFF00FF")       ' Hex of colors as String
'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' Hex Values of colors
'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' IndexColor
Clr = Array(0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 255, 0, 255, 0, 255)
With Charts.Add
    .ChartType = xlXYScatterLinesNoMarkers
    .SetSourceData Source:=Sheets("Plot").Range("A12:E213")
    j = 0
    For i = 2 To 5
        With .FullSeriesCollection(i - 1).Format.Line
            .ForeColor.RGB = RGB(Clr(j), Clr(j + 1), Clr(j + 2))
            j = j + 3
        End With
    Next
End With
   
End Sub

The code below works and sets the color of the four lines with the three values for rgb. The three rgb values are filled in an array (Clr) which is not very clear. Is there an alternative way for instant by using Hex numbers, ColorIndex or the predefined colors like vbBlack?

Sub CreateChart()
Dim i As Long
Dim j As Long
Dim Clr() As Variant

'Clr = Array("vbBlack", "vbBlue", "vbRed", "vbCyan", "vbGreen", "vbMagenta")    ' colors as types
'Clr = Array("&H0", "&HFF0000", "&Hff", "&HFFFF00", "&HFF00", "&HFF00FF")       ' Hex of colors as String
'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' Hex Values of colors
'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' IndexColor
Clr = Array(0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 255, 0, 255, 0, 255)
With Charts.Add
    .ChartType = xlXYScatterLinesNoMarkers
    .SetSourceData Source:=Sheets("Plot").Range("A12:E213")
    j = 0
    For i = 2 To 5
        With .FullSeriesCollection(i - 1).Format.Line
            .ForeColor.RGB = RGB(Clr(j), Clr(j + 1), Clr(j + 2))
            j = j + 3
        End With
    Next
End With
   
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2025-02-02 21:30:58

实际上,您的代码效果很好,只需摆脱引号和 RGB 函数即可。同样,十六进制值没有错,它们工作正常。

Sub CreateChart()
Dim Color As Variant
Dim Source as Range
Dim i As Long

    Set Source = Sheets("Plot").Range("A12:E213")
    Color = Array(vbBlack, vbBlue, vbRed, vbCyan, vbGreen, vbMagenta)
    ' Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF) works as well
    With Charts.Add
        .ChartType = xlXYScatterLinesNoMarkers
        .SetSourceData Source
        For i = 1 To .FullSeriesCollection.Count
            .FullSeriesCollection(i).Format.Line.ForeColor.RGB = Color(i - 1)
        Next i
    End With

End Sub

要使用颜色(i),而不是某种笨拙的颜色(i -1)我们应该添加选项base 1 start array 默认情况下的索引。但这当然取决于您。

ps查看 xlrgbbcolor

Actually, your code works well, just get rid of quotation marks and RGB function. Also there's nothing wrong with hex values, they work fine.

Sub CreateChart()
Dim Color As Variant
Dim Source as Range
Dim i As Long

    Set Source = Sheets("Plot").Range("A12:E213")
    Color = Array(vbBlack, vbBlue, vbRed, vbCyan, vbGreen, vbMagenta)
    ' Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF) works as well
    With Charts.Add
        .ChartType = xlXYScatterLinesNoMarkers
        .SetSourceData Source
        For i = 1 To .FullSeriesCollection.Count
            .FullSeriesCollection(i).Format.Line.ForeColor.RGB = Color(i - 1)
        Next i
    End With

End Sub

To use Color(i) instead of somewhat clumsy Color(i - 1) we should add Option Base 1 to start Array index from one by default. But it's up to you, of course.

P.S. Take a look at xlRGBColor enumeration

我纯我任性 2025-02-02 21:30:58

使用 nofollow noreferrer“> hex2dec()

Sub CreateChart()
    Dim i As Long
    Dim j As Long
    Dim Clr() As Variant

    'Clr = Array("vbBlack", "vbBlue", "vbRed", "vbCyan", "vbGreen", "vbMagenta")    ' colors as types
    'Clr = Array("&H0", "&HFF0000", "&Hff", "&HFFFF00", "&HFF00", "&HFF00FF")       ' Hex of colors as String
    Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' Hex Values of colors
    'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' IndexColor
    'Clr = Array(0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 255, 0, 255, 0, 255)
    With Charts.Add
        .ChartType = xlXYScatterLinesNoMarkers
        .SetSourceData Source:=Sheets("Plot").Range("A12:E213")
        j = 0
        For i = 2 To 5
            With .FullSeriesCollection(i - 1).Format.Line
                .ForeColor.RGB = WorksheetFunction.Hex2Dec(Clr(j))
                j = j + 1
            End With
        Next i
    End With
End Sub

Use the Hex2Dec() WorkSheetFunction ?

Sub CreateChart()
    Dim i As Long
    Dim j As Long
    Dim Clr() As Variant

    'Clr = Array("vbBlack", "vbBlue", "vbRed", "vbCyan", "vbGreen", "vbMagenta")    ' colors as types
    'Clr = Array("&H0", "&HFF0000", "&Hff", "&HFFFF00", "&HFF00", "&HFF00FF")       ' Hex of colors as String
    Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' Hex Values of colors
    'Clr = Array(&H0, &HFF0000, &HFF, &HFFFF00, &HFF00, &HFF00FF)                   ' IndexColor
    'Clr = Array(0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 255, 255, 0, 255, 0, 255, 0, 255)
    With Charts.Add
        .ChartType = xlXYScatterLinesNoMarkers
        .SetSourceData Source:=Sheets("Plot").Range("A12:E213")
        j = 0
        For i = 2 To 5
            With .FullSeriesCollection(i - 1).Format.Line
                .ForeColor.RGB = WorksheetFunction.Hex2Dec(Clr(j))
                j = j + 1
            End With
        Next i
    End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文