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

reg*_*gie 2 excel vba graph series excel-vba

我试图从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
Run Code Online (Sandbox Code Playgroud)

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

我怎么循环系列并删除那些空的?

chr*_*sen 5

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

For i = .SeriesCollection(Series).points.count To 1 Step -1
Run Code Online (Sandbox Code Playgroud)

这样.Delete就不会影响尚未循环的项目