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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5624882/
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-08 11:21:07  来源:igfitidea点击:

Changing bar colors in bar graph

excelvbagraphcolorsbar-chart

提问by ScottyStyles

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 onlything I cannot figure out is how to change the colors of the bargraphs.

我已经为 Excel 2007 程序创建了一个 VBA,它可以根据活动工作簿中多达 52 个不同的选项卡自动创建 ROI 的条形图。我快完成了,我唯一无法弄清楚的是如何更改条形图的颜色。

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 Withto set this, but I have spent far too much time trying to figure it out, only to come up with nothing.

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

回答by Jubbles

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)?

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

回答by Csaba

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.

它在非常相似的情况下适用于我 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)

回答by acadie

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)
...