2016-03-06 109 views
0

我想在Excel VBA中編寫一個宏,它添加了一個圖表,然後想重新命名並編輯列的顏色,但不知何故它會引發調試錯誤。在Excel VBA中編輯和命名新添加的圖表

這是我的代碼。是否有人可以幫助我:

Sub Charts() 

ActiveSheet.Shapes.AddCha rt.Select 
ActiveChart.ChartType = xlColumnStacked100 
ActiveChart.SetSourceData Source:=Sheets("Calculations").Range("A1:D11") 
ActiveChart.Name = "MyChart" 
ActiveChart.SeriesCollection(1).XValues = "=Data!$N$5:$N$14" 
ActiveChart.SeriesCollection(3).Select 
    ActiveChart.Legend.Select 
    ActiveChart.Legend.LegendEntries(1).Select 
    With Selection.Format.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
    End With 
    ActiveChart.Legend.LegendEntries(2).Select 
    With Selection.Format.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 192, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
    End With 
    ActiveChart.Legend.LegendEntries(3).Select 
    With Selection.Format.Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
     .Solid 
    End With 
    With Selection.Format.Line 
     .Visible = msoTrue 
     .ForeColor.ObjectThemeColor = msoThemeColorText1 
     .ForeColor.TintAndShade = 0 
     .ForeColor.Brightness = 0 
     .Transparency = 0 
    End With 
    ActiveChart.SeriesCollection(3).Select 
    ActiveChart.Axes(xlValue).MajorGridlines.Select 
    Selection.Delete 
    End Sub 

感謝

+0

哪一行產生錯誤,錯誤到底是什麼? – vacip

+0

對於此:ActiveChart.Name =「MyChart」,錯誤是:指定的維度對當前圖表類型無效。 – user1778266

+0

對於此:使用Selection.Format.Fill,錯誤是對象'ChartFormat'的方法'填充'失敗 – user1778266

回答

1

所以,請將.Name屬性只能用於圖表工作表設置。對於嵌入式圖表(圖表對象)它是隻讀的,因此您無法爲其分配值。您可以爲其容器的名稱指定一個值:

ActiveChart.Parent.Name = "MyChart" 

而不是嘗試格式化圖例條目,自己格式化系列。我也重寫了您的.with語句,在格式化之前不需要選擇每個項目:

Sub ChartThingy() 

ActiveSheet.Shapes.AddChart.Select 
ActiveChart.ChartType = xlColumnStacked100 
ActiveChart.SetSourceData Source:=Sheets("Calculations").Range("A1:D11") 
ActiveChart.Parent.Name = "MyChart" 
ActiveChart.SeriesCollection(1).XValues = "=Data!$N$5:$N$14" 
    With ActiveChart.SeriesCollection(3).Format 
    With .Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With .Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
    End With 
    End With 
    With ActiveChart.SeriesCollection(2).Format 
    With .Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 192, 0) 
     .Transparency = 0 
     .Solid 
    End With 
    With .Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 0, 0) 
     .Transparency = 0 
    End With 
    End With 
    With ActiveChart.SeriesCollection(1).Format 
    With .Fill 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(0, 176, 80) 
     .Transparency = 0 
     .Solid 
    End With 
    With .Line 
     .Visible = msoTrue 
     .ForeColor.ObjectThemeColor = msoThemeColorText1 
     .ForeColor.TintAndShade = 0 
     .ForeColor.Brightness = 0 
     .Transparency = 0 
    End With 
    End With 
    ActiveChart.Axes(xlValue).MajorGridlines.Select 
    Selection.Delete 
End Sub 
+0

工作!非常感謝 :) – user1778266