StackJP
StackJP

Reputation: 681

VBA - Chart Color Doesn't Change

I have this code to create a chart:

Sub CreateChart()
    Dim rng As Range
    Dim cht As Object
    Set rng = ActiveSheet.Range("A4:C8")
    Set cht = ActiveSheet.Shapes.AddChart2
    cht.Chart.SetSourceData Source:=rng, PlotBy:=xlColumns
    cht.Chart.ChartType = xlBarStacked
    cht.SeriesCollection(1).Interior.Color = RGB(255, 255, 255)
End Sub

But the series 1 bars are not changing the color.

Can you help?

Thanks!

Upvotes: 0

Views: 1954

Answers (2)

dudu721
dudu721

Reputation: 340

Try this :

Sub CreateChart()
Dim rng As Range
Set rng = ActiveSheet.Range("A4:C8")
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
    .SetSourceData Source:=Range("Feuil1!$A$4:$B$8")
    .SeriesCollection.NewSeries
    .SetSourceData Source:=rng, PlotBy:=xlColumns
    .ChartType = xlBarStacked
    .SeriesCollection(1).Interior.ColorIndex = 5  'Change value to change color
    End With
End Sub

and choose the value in the color index table. 5 correspond to blue. You can get the color index table here .

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33682

See answer below, it implements what you wanted in your post, in a different method, allowing you more flexibility in the future:

Option Explicit

Sub CreateChart()

Dim rng             As Range
Dim cht             As ChartObject
Dim cht_Series      As Series

Set rng = ActiveSheet.Range("A4:C8")

' in brackets (Left, Width, Top, Height) >> modify according to your needs
Set cht = ActiveSheet.ChartObjects.Add(100, 100, 100, 100)

With cht
    .Chart.SetSourceData Source:=rng
    .Chart.PlotBy = xlColumns
    .Chart.ChartType = xlBarStacked
End With

Set cht_Series = cht.Chart.SeriesCollection(1)
' this will result to white (by your post) >> modify to your desired color
cht_Series.Format.Fill.ForeColor.RGB = RGB(255, 255, 255)

End Sub

Upvotes: 1

Related Questions