从图表中删除空系列(使用VBA)

时间:2011-10-24 12:04:18

标签: excel vba excel-vba graph series

我正在尝试从Excel图表中删除所有空系列。

    Dim isEmptySeries As Boolean
    For Series = 1 To .SeriesCollection.count
        .SeriesCollection(Series).ApplyDataLabels Type:=xlDataLabelsShowValue, AutoText:=True, LegendKey:=False
        isEmptySeries = True

        For i = 1 To .SeriesCollection(Series).points.count
            If .SeriesCollection(Series).points(i).DataLabel.Text = 0 Then
                .SeriesCollection(Series).points(i).HasDataLabel = False
            Else
                isEmptySeries = False
                .SeriesCollection(Series).points(i).DataLabel.Font.Size = 17
            End If
        Next i

        If isEmptySeries Then
                .SeriesCollection(Series).Delete
        End If
    Next Datenreihe

脚本在ApplyDatalabels行失败(“Method SeriesCollection of Object Chart failed”)。 我相信当删除其中一个系列时,Excel会移动系列索引?是这样的吗?这是我对错误的唯一解释。

我如何循环播放该系列并删除那些空的?

1 个答案:

答案 0 :(得分:5)

在这些情况下尝试以相反的顺序循环

For i = .SeriesCollection(Series).points.count To 1 Step -1

这样.Delete不会影响尚未通过

循环的项目