从现有的xy图删除数据系列

时间:2020-07-13 07:05:25

标签: excel vba excel-charts

我想创建一个代码,从工作表上的现有xy图表中删除数据系列。 但是我不知道这段代码怎么了。有人请帮助我。

Dim srs As Series
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveChart
    For Each srs In .SeriesCollection
        If .Name = c Then
        .SeriesCollection(srs).Delete
        End If
    Next
End With

1 个答案:

答案 0 :(得分:0)

当与[Object] ... End With一起使用时,所有有效的点方法或属性都将附加到该[Object]

With ActiveChart
    For Each srs In .SeriesCollection
        If .Name = c Then
        .SeriesCollection(srs).Delete
        End If
    Next
End With

实际上,它将变为:

For Each srs In ActiveChart.SeriesCollection
    If ActiveChart.Name = c Then
         ActiveChart.SeriesCollection(srs).Delete
    End If
Next

我认为ActiveChart.Name不是要在您的IF语句中进行比较的预期属性。

您可以在IF中尝试“ srs.Name = c”,并在其中尝试“ srs.Delete”:

With ActiveChart
    For Each srs In .SeriesCollection
        If srs.Name = c Then
           srs.Delete
        End If
    Next
End With

请告诉我是否可行。