【问题标题】:Changing color of Bars in a bar chart更改条形图中条形的颜色
【发布时间】:2012-01-19 17:25:42
【问题描述】:

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

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

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

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

非常感谢

【问题讨论】:

    标签: excel charts excel-2007 vba


    【解决方案1】:

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

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

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

    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
    

    【讨论】:

    • 您好,Doug,感谢您的回复。第一部分效果很好,但第二部分给了我一个错误。 “运行时错误 '451':未定义属性让过程和属性获取过程未返回对象。”