更改图表边框颜色添加为Shape对象VBA Excel

时间:2017-05-09 04:50:41

标签: vba excel-vba excel

我正在尝试更改作为形状添加的图表的边框颜色。我尝试使用宏录制器,但添加到我自己的代码时代码不起作用。它引用的Shape对象存在一些问题。有人可以帮帮我吗?

用于将图表添加到Excel的代码:

Set Cht = ActiveSheet.Shapes.AddChart(Left:=, Width:=, Top:=, Height:=).Chart

Macro Recorder录制的代码:

With ActiveSheet.Shapes("Chart 1").Line 'Line and pattern color
    .Visible = msoTrue
    .ForeColor.ObjectThemeColor = msoThemeColorAccent1
    .ForeColor.TintAndShade = 0
    .ForeColor.Brightness = 0
    .Transparency = 0
    .Weight = 2
End With
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveSheet.Shapes("Chart 1").Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.8000000119
        .Transparency = 0
        .Solid
End With

令人惊讶的是,该代码适用于Chart1,但在移动到Chart2时无法工作。如果您需要其他详细信息,请与我们联系。

由于

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码循环浏览所有Shapes,如果形状类型为msoChart,则修改边框和填充属性。

<强>代码

Option Explicit

Sub FormatMultiCharts()

Dim MyCht       As Shape

For Each MyCht In ActiveSheet.Shapes
    If MyCht.Type Like msoChart Then ' check if shape type is chart
        With MyCht.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0
            .Transparency = 0
            .Weight = 2
        End With

        With MyCht.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0.8000000119
            .Transparency = 0
            .Solid
        End With
    End If
Next MyCht

End Sub

答案 1 :(得分:0)

黑色边框图:1 ==黑色

使用ActiveChart.ChartArea   .Border.ColorIndex = 1 结尾为