Changing color of Bars in a bar chart

余生颓废 提交于 2019-12-24 08:38:38

问题


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.

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.

Thanks Very much


回答1:


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

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


来源:https://stackoverflow.com/questions/8495987/changing-color-of-bars-in-a-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!