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

时间:2022-09-15 08:56:05

I am trying to remove all empty series out of an Excel graph.

我试图从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

The script fails at the ApplyDatalabels line ("Method SeriesCollection of Object Chart failed"). I believe that Excel shifts the Series indexes when one of the Series is deleted? Is that the case? It's the only explanation that I have for the error.

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

How else would I loop through the series and remove the ones that are empty?

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

1 个解决方案

#1


5  

In these sorts of situations try looping in reverse order

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

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

That way the .Delete doesn't effect the item yet to be looped through

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

#1


5  

In these sorts of situations try looping in reverse order

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

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

That way the .Delete doesn't effect the item yet to be looped through

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