【发布时间】:2021-06-11 16:48:08
【问题描述】:
我有一个动态图表,我在清除数据序列时遇到了困难。
据我了解,数据序列索引是累积的。意思是,如果我从另一张干净地制作了 12 个数据系列的工作表中复制“图表 4”。 dataseries 的数量 = 12 = ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count 并且这些系列的索引从 1 到 12。
现在,如果我删除一个系列并添加一个,数据系列的数量将继续为 12,但索引现在将从 1-11 和 13 运行。
因此,当我尝试通过计算系列数量并删除索引为 1 的系列来删除它们时:ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count 如果系列已被删除和添加,它将失败。
为了克服这个问题,我尝试了“for each.. in chart 4..”选项:
For Each Series In ActiveSheet.ChartObjects("Chart 4")
'For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4")
ActiveChart.FullSeriesCollection.Delete
Next
我收到一条错误消息:
“对象不支持此属性或方法”
我在stackoverflow上查看了这里的问题,发现计数器需要下降:
我从VBA deleting chart series复制调整:
Dim iSrs As Long
With ActiveChart
For iSrs = .SeriesCollection.count To 1 Step -1
If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then
.SeriesCollection(iSrs).Delete
End If
Next
End With
MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
这不会删除所有数据系列,因为之后它仍然显示:count = 27。
我尝试了其他几种公式,结果不是全部删除,(有时“on error resume next”打开,它会删除一半,奇数时向下舍入)完整的代码是:
'select workbook, worksheet
Workbooks("N.xlsm").Worksheets("day_visual").Activate
Workbooks("N.xlsm").Worksheets("day_visual").range("A1").Select
'select chart
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveSheet.ChartObjects("Chart 4").Select
'remove all series(0 to xx?)
MsgBox (ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
'For Remove = 1 To ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count
' 'On Error Resume Next
' ActiveChart.FullSeriesCollection(Remove).Select
' Selection.Delete
'
' 'ActiveChart.FullSeriesCollection(Remove).Delete
' 'MsgBox ("hi")
' count_non_existant_series = 1 + count_non_existant_series
'Next Remove
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveSheet.ChartObjects("Chart 4").Select
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").SeriesCollection.count To 2 Step -1
'For x = Workbooks("N.xlsm").Worksheets("day_visual").ChartObjects("Chart 4").FullSeriesCollection.count To 2 Step -1
' ActiveSheet.ChartObjects("Chart 4").SeriesCollection(x).Delete
'Next x
Dim iSrs As Long
With ActiveChart
For iSrs = .SeriesCollection.count To 1 Step -1
If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then
.SeriesCollection(iSrs).Delete
End If
Next
End With
'For Each Series In ActiveSheet.ChartObjects("Chart 4")
For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4")
ActiveChart.FullSeriesCollection.Delete
Next
MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.count)
'With ActiveSheet.ChartObjects("Chart 4")
''Do While .SeriesCollection.count >= 1
'.SeriesCollection(.SeriesCollection.count).Delete
'Loop
'End With
Dim add_chartlabels As Long
我缺乏对 Excel 如何存储索引的理解,导致我尝试不正确的解决方案。
- 我对图表系列指数的理解有误吗?
- 为什么“for each”方法会产生运行时 483 错误?
- 为什么手动迭代代码不删除所有系列?
- 如何从图表中删除所有系列而不删除 图表本身?
【问题讨论】:
-
#3 -- 你只是删除一个系列如果系列名称包含子字符串
"series"。 -
@a.t.您是否尝试过以下任何答案?有什么反馈吗?