Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 图例格式上录制的宏中断_Excel_Vba - Fatal编程技术网

Excel 图例格式上录制的宏中断

Excel 图例格式上录制的宏中断,excel,vba,Excel,Vba,我多次尝试重新创建此宏以使其符合我的需要,因此我运行了一次来测试它,当它到达带有Selection.Format.Line的时,它就坏了。宏的哪个部分导致此错误 编辑:Shai Rado发布了完美修复下面的代码将“图表19”的图表对象设置为一个变量,然后只需修改所需的参数,而无需使用活动图表,选择和选择 Sub Total() ActiveChart.ClearToMatchStyle ActiveChart.ChartStyle = 227 ActiveSheet.ChartObjects("

我多次尝试重新创建此宏以使其符合我的需要,因此我运行了一次来测试它,当它到达带有Selection.Format.Line的
时,它就坏了。宏的哪个部分导致此错误


编辑:Shai Rado发布了完美修复

下面的代码将“图表19”的
图表对象
设置为一个变量,然后只需修改所需的参数,而无需使用
活动图表
选择
选择

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
选项显式
小计()
将ChtObj设置为图表对象

Set ChtObj=Worksheets(“Sheet4”).ChartObjects(“图表19”)愚蠢的回答:带有Selection.Format.Line的
正在破坏您的代码。看起来您从“图表19”开始,选择图例,然后选择第一个图例条目。确保确实存在图例条目,并且LegendEntries集合基于1(而不是0),您可以将其与ActiveChart一起使用。legend.LegendEntries(5)。Format.Line,删除所选内容。选择“原因错误”图例中是否至少有5个条目?@Lowpar优秀点。有关避免
.Select
:@CodeJockey抱歉,我在发布问题之前尝试删除Select,相同的故事。我使用ActiveChart.Legend.LegendEntries(1.Format.Line)执行了
,但得到了相同的错误。不仅如此,为了测试出现错误的地方,重新打开工作簿的未编辑副本,并将代码粘贴回模块中,以强制vba执行宏,就像我在录制时执行宏一样,然后宏就坏了。
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