logging的macros在图例格式设置中断

Sub Total() ActiveChart.ClearToMatchStyle ActiveChart.ChartStyle = 227 ActiveSheet.ChartObjects("Chart 19").Activate ActiveChart.Legend.Select ActiveChart.Legend.LegendEntries(1).Select With Selection.Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0 End With ActiveChart.Legend.LegendEntries(5).Select With Selection.Format.Line .Visible = msoTrue .ForeColor.RGB = RGB(255, 255, 0) .Transparency = 0 End With ActiveChart.Legend.LegendEntries(6).Select With Selection.Format.Line .Visible = msoTrue .ForeColor.RGB = RGB(0, 176, 80) .Transparency = 0 End With End Sub 

我试图重新创build这个macros多次模型到我的需要,所以我跑了一次来testing它,它打破了一旦它到了With Selection.Format.Line , 给我这个错误 。 macros的哪个部分导致了这个错误?

编辑:Shai Rado张贴完美的修复在图例格式化logging的macrosrest

下面的代码将“Chart 19”的ChartObject设置为一个variables,稍后只需修改所需的参数,而无需使用ActiveChartSelectionSelect

 Option Explicit Sub Total() Dim ChtObj As ChartObject Set ChtObj = Worksheets("Sheet4").ChartObjects("Chart 19") '<-- modify "Sheet4" to your sheet's name where you have your charts With ChtObj With .Chart.SeriesCollection(1).Format.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText1 .Transparency = 0 End With With .Chart.SeriesCollection(5).Format.Line .Visible = msoTrue .ForeColor.RGB = RGB(255, 255, 0) .Transparency = 0 End With With .Chart.SeriesCollection(6).Format.Line .Visible = msoTrue .ForeColor.RGB = RGB(0, 176, 80) .Transparency = 0 End With End With End Sub