更改条形图中的条形颜色

发布于 2024-11-01 02:14:18 字数 1308 浏览 3 评论 0原文

我创建了一个 VBA for Excel 2007 程序,该程序根据活动工作簿中最多 52 个不同的选项卡自动创建 ROI 条形图。我即将完成,我唯一不明白的是如何更改条形图的颜色。

这些图表是在它们自己的子函数中创建的,通过像这样的调用来调用。每个变量在被调用时都会发生变化。

Call AddChartObject(1, 1, "Example", extraWeeks, weekDifference)

我的子系统看起来像这样。

Sub AddChartObject(j As Integer, k As Integer, passedChartTitle As String, xtraWks As Integer, ttlWks As Integer)

    Dim topOfChart As Integer

    topOfChart = 25 + (350 * j)

    'Adds bar chart for total sales

    With ActiveSheet.ChartObjects.Add(Left:=375, Width:=475, Top:=topOfChart, Height:=325)
        .Chart.SetSourceData Source:=Sheets("Consolidation").Range("$A$" & 3 + ((17 + xtraWks) _
            * j) & ":$C$" & (4 + ttlWks) + ((17 + xtraWks) * k))
        .Chart.ChartType = xl3DColumnClustered
        .Chart.SetElement (msoElementDataLabelShow)
        .Chart.HasTitle = True
        .Chart.ChartTitle.Text = passedChartTitle & " Sales"
        .Chart.SetElement (msoElementLegendBottom)
        .Chart.SetElement (msoElementDataLabelNone)
        .Chart.RightAngleAxes = True
    End With

End Sub

根据营销人员的意愿,我想在条形图中第二个系列上使用的 RGB 颜色是 (155, 187, 89)。我很确定有一个 .chart。???.???? = RGB (155, 187, 89) 命令我可以在我的 With 中使用它来设置它,但我花了太多时间试图弄清楚它,只是想出了没有什么。

I've created a VBA for Excel 2007 program that automatically creates bar graphs for ROI based on up to 52 different tabs in the active workbook. I'm close to done, and the only thing I cannot figure out is how to change the colors of the bargraphs.

The graphs are created in their own subfunction, called with a call like so. Every variable changes around whenever it's called.

Call AddChartObject(1, 1, "Example", extraWeeks, weekDifference)

My sub that it calls looks like this.

Sub AddChartObject(j As Integer, k As Integer, passedChartTitle As String, xtraWks As Integer, ttlWks As Integer)

    Dim topOfChart As Integer

    topOfChart = 25 + (350 * j)

    'Adds bar chart for total sales

    With ActiveSheet.ChartObjects.Add(Left:=375, Width:=475, Top:=topOfChart, Height:=325)
        .Chart.SetSourceData Source:=Sheets("Consolidation").Range("$A$" & 3 + ((17 + xtraWks) _
            * j) & ":$C$" & (4 + ttlWks) + ((17 + xtraWks) * k))
        .Chart.ChartType = xl3DColumnClustered
        .Chart.SetElement (msoElementDataLabelShow)
        .Chart.HasTitle = True
        .Chart.ChartTitle.Text = passedChartTitle & " Sales"
        .Chart.SetElement (msoElementLegendBottom)
        .Chart.SetElement (msoElementDataLabelNone)
        .Chart.RightAngleAxes = True
    End With

End Sub

The RGB color I want to use on the SECOND series in the bar chart is (155, 187, 89), per marketing's wishes. I'm pretty sure there is a .chart.????.???? = RGB (155, 187, 89) command I can use in my With to set this, but I have spent far too much time trying to figure it out, only to come up with nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时模样 2024-11-08 02:14:19

它适用于我的 ScottyStyles,情况非常相似,但仅适用于第一个系列集合。我在下面使用了相同的内容,并且没有改变 SeriesCollection(2) 的颜色。那是一组线性数据。

ActiveSheet.ChartObjects("Chart 1").Activate

    ActiveChart.ClearToMatchStyle

    ActiveChart.SeriesCollection(1).Interior.Color = RGB(85, 142, 213)
    ActiveChart.SeriesCollection(2).Interior.Color = RGB(192, 0, 0)

It works for me ScottyStyles in a very similar situation, but only for the first series collection. I used the same right below that, and that was not changing the color of the SeriesCollection(2). That one is a linear set of datas.

ActiveSheet.ChartObjects("Chart 1").Activate

    ActiveChart.ClearToMatchStyle

    ActiveChart.SeriesCollection(1).Interior.Color = RGB(85, 142, 213)
    ActiveChart.SeriesCollection(2).Interior.Color = RGB(192, 0, 0)
老旧海报 2024-11-08 02:14:19

要更改集合中的不同条形,您可以使用:

ActiveChart.SeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = RGB(85, 142, 213)
ActiveChart.SeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = RGB(192,0, 0)
...

to change different bars inside a collection you can use:

ActiveChart.SeriesCollection(1).Points(1).Format.Fill.ForeColor.RGB = RGB(85, 142, 213)
ActiveChart.SeriesCollection(1).Points(2).Format.Fill.ForeColor.RGB = RGB(192,0, 0)
...

嘿咻 2024-11-08 02:14:18

您是否尝试过

.Chart.SeriesCollection([index]).Interior.Color = RGB(155, 187, 89)

(其中 [index] 是您要更改颜色的系列的占位符)?

Have you tried

.Chart.SeriesCollection([index]).Interior.Color = RGB(155, 187, 89)

(where [index] is a placeholder for the series you want to change the color for)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文