0

I need to change the background image of a chart I created using vba. I tried using the .Fill command but I can't make it work. How do I do it? This is the code I used to create the chart and it is working fine:

With myChart
    .ChartStyle = 245
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = ""
    .HasLegend = False
    .Axes(xlCategory).HasTitle = True
    .Axes(xlCategory).AxisTitle.Text = "Punteggio Tecnico"
    .Axes(xlValue).HasTitle = True
    .Axes(xlValue).AxisTitle.Characters.Text = "Punteggio Economico"
    .Axes(xlCategory).MinimumScale = 0
    .Axes(xlCategory).MaximumScale = 1
    .Axes(xlValue).MinimumScale = 0
    .Axes(xlValue).MaximumScale = 1
    .SeriesCollection(1).XValues = "=Dati2!B3:B270"
    .SeriesCollection(1).Values = "=Dati2!C3:C270"
End With
2
  • Can you share exactly how you are using .Fill and how it's not working? Commented Dec 13, 2018 at 16:13
  • 1
    @J.schmidt The Excel-VBA tag is pending removal so shouldn't be used. Commented Dec 13, 2018 at 16:23

1 Answer 1

1

If myChart is Chart variable then you want the myChart.PlotArea.Format.Fill for the area of the chart that contains the actual chart, or myChart.ChartArea.Format.Fill for the whole chart area.

The code below shows how to use it. I've commented out the colouring code that a macro recorder would supply and replaced with a basic RGB value.

Sub Test()

    Dim myChart As Chart

    Set myChart = Sheet1.ChartObjects("Chart 2").Chart

    With myChart
        With .PlotArea.Format.Fill
            .ForeColor.RGB = RGB(255, 0, 0)

'            .Visible = msoTrue
'            .ForeColor.ObjectThemeColor = msoThemeColorAccent6
'            .ForeColor.TintAndShade = 0
'            .ForeColor.Brightness = 0.400000006
'            .Solid
        End With

        With .ChartArea.Format.Fill
            .ForeColor.RGB = RGB(0, 255, 0)

'            .Visible = msoTrue
'            .ForeColor.ObjectThemeColor = msoThemeColorAccent6
'            .ForeColor.TintAndShade = 0
'            .ForeColor.Brightness = 0.400000006
'            .Transparency = 0
'            .Solid
        End With

    End With

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Good stuff. Feel free to tick as accepted answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.