2017-05-09 213 views
0

我試圖更改作爲形狀添加的圖表的邊框顏色。我嘗試過使用宏錄製器,但添加到我自己的代碼時,代碼不起作用。它與它所引用的Shape對象有一些問題。有人能幫助我嗎?用於圖表添加到Excel中更改圖表添加爲Shape對象的邊框顏色VBA Excel

代碼:由宏錄製記錄

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

代碼:

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工作。讓我知道你是否需要更多細節。

感謝

+0

哪裏是圖2的代碼?它在哪裏崩潰? – jivko

+0

@Ayush你想更新多個圖表嗎? –

+0

感謝您關注此事。 @Shai Rado - 是的,我在一張紙上有3個圖表,我需要將這些格式更改應用於所有這些更改。 – Ayush

回答

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 
+0

這是完美的!非常感謝! – Ayush