如何在VBA中更改图表的背景图像?

时间:2018-12-13 16:08:26

标签: excel vba charts

我需要更改使用vba创建的图表的背景图像。我尝试使用.Fill命令,但无法正常工作。我该怎么做? 这是我用来创建图表的代码,并且运行良好:

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

1 个答案:

答案 0 :(得分:1)

如果myChartChart变量,则需要myChart.PlotArea.Format.Fill代表包含实际图表的图表区域,或myChart.ChartArea.Format.Fill代表整个图表区域。

下面的代码显示了如何使用它。我已经注释掉了宏记录器将提供的着色代码,并将其替换为基本的RGB值。

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