简体   繁体   中英

How do I change the background image of a chart in vba?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM