vba 更改条形图中条形的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8495987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:41:56  来源:igfitidea点击:

Changing color of Bars in a bar chart

excel-vbachartsexcel-2007vbaexcel

提问by Nupur

I have a code in excel to change colors of bar graph but its not working. Can anyone suggest me what I am doing wrong in the code.

我在 excel 中有一个代码来更改条形图的颜色,但它不起作用。任何人都可以建议我在代码中做错了什么。

With ActiveChart.SeriesCollection(1).Interior.Color = RGB(0, 153, 64)
End With

This code doesnt affect color of the bar.

此代码不会影响条形的颜色。

Also, For all bars (representing values 0 to 200) I want one color (green) but for two bars representing two data points (100 and 200), I want to add different color. Can anyone please tell me how to to that with VBA. I would appreciate your time regarding the same.

此外,对于所有条(代表 0 到 200 的值),我想要一种颜色(绿色),但对于代表两个数据点(100 和 200)的两个条,我想添加不同的颜色。谁能告诉我如何用 VBA 做到这一点。我很感激你的时间。

Thanks Very much

非常感谢

回答by Doug Glancy

The With statement specifies the objects or properties to be acted on. Your code should be like this:

With 语句指定要对其执行操作的对象或属性。你的代码应该是这样的:

With ActiveChart.SeriesCollection(1)
    .Interior.Color = RGB(0, 153, 64)
End With

EDIT - For the 2nd part of your question:

编辑 - 对于您问题的第二部分:

Sub ColorBars()
Dim chtSeries As Excel.Series
Dim i As Long

For Each chtSeries In ActiveChart.SeriesCollection
    With chtSeries
        For i = 1 To .Points.Count
            If .Values(i) = 100 Or .Values(i) = 200 Then
                .Points(i).Interior.Color = .Interior.Color = RGB(75, 172, 198)
            Else
                .Points(i).Interior.Color = RGB(0, 153, 64)
            End If
        Next i
    End With
Next chtSeries
End Sub