如何在VBA中更改多个图表的颜色?

我想要更改图表上不同列的颜色,我有大约50个图表,所以我宁愿不要手动执行此操作。

我看了其他论坛,并看到使用:

For Each cht In Sheets("GraphData").ChartObjects 

当我运行时,我得到一个types不匹配的错误

我不太明白为什么它不工作。 任何帮助,将不胜感激。 谢谢。

这是我的代码:

 Sub changestuff() Dim cht As Chart, ChtObj As ChartObject, wks As Worksheet For Each cht In Sheets("GraphData").ChartObjects cht.Legend.Delete cht.SeriesCollection(1).Points(2).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(146, 208, 80) .Transparency = 0 .Solid End With cht.SeriesCollection(1).Points(3).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 255, 0) .Transparency = 0 .Solid End With cht.SeriesCollection(1).Points(4).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 192, 0) .Transparency = 0 .Solid End With cht.SeriesCollection(1).Points(5).Select With Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With Next End Sub 

types不匹配是因为Sheets("GraphData").ChartObjects是ChartObjects而不是Chart。 它看起来像你想循环的顶部

 For Each ChtObj In Sheets("GraphData").ChartObjects set cht = ChtObj.chart 

这是我的testing,摆脱了不匹配的错误。

你可以从一个Variant或一个Object开始,从一个迭代器中testing你将返回的Object ,然后你可以改变你的声明。