VBA更改图表的颜色与ColorIndex
下面的代码可以使用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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您的代码效果很好,只需摆脱引号和
RGB
函数即可。同样,十六进制值没有错,它们工作正常。要使用
颜色(i)
,而不是某种笨拙的颜色(i -1)
我们应该添加选项base 1
startarray默认情况下的索引。但这当然取决于您。
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.To use
Color(i)
instead of somewhat clumsyColor(i - 1)
we should addOption Base 1
to startArray
index from one by default. But it's up to you, of course.P.S. Take a look at xlRGBColor enumeration
使用 nofollow noreferrer“> hex2dec()
Use the Hex2Dec() WorkSheetFunction ?