从图中删除空的系列(使用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 

该脚本在ApplyDatalabels行(“对象图的方法SeriesCollection失败”)失败。 我相信当Excel中的其中一个系列被删除时,Excel会改变系列索引? 是这样吗? 这是我对错误的唯一解释。

除此之外,我还将如何循环播放这些内容,并删除那些空的内容?

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

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

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