Deleting empty Series out of Graph (with VBA)

前提是你 提交于 2019-12-24 05:23:07

问题


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

    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.

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


回答1:


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



来源:https://stackoverflow.com/questions/7875326/deleting-empty-series-out-of-graph-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!