Excel 删除图表中的数据系列
我有一个动态图表,我很难清除数据序列 据我所知,dataseries指数是累积的。也就是说,如果我从另一张有12个数据系列的纸上复制“图表4”的话。dataseries的数量=12=ActiveSheet.ChartObjects(“图表4”).Chart.SeriesCollection.count,这些系列的索引从1到12 现在,如果我删除一个系列并添加一个,数据系列的数量将继续是12,但索引现在将从1-11和13运行 因此,当我试图通过计算系列的数量并删除索引为1的系列来删除它们时:ActiveSheet.ChartObjects(“图表4”).Chart.SeriesCollection.count如果删除并添加了系列,则将失败 为了克服这个问题,我尝试了图4中的“for each.”选项:Excel 删除图表中的数据系列,excel,vba,charts,Excel,Vba,Charts,我有一个动态图表,我很难清除数据序列 据我所知,dataseries指数是累积的。也就是说,如果我从另一张有12个数据系列的纸上复制“图表4”的话。dataseries的数量=12=ActiveSheet.ChartObjects(“图表4”).Chart.SeriesCollection.count,这些系列的索引从1到12 现在,如果我删除一个系列并添加一个,数据系列的数量将继续是12,但索引现在将从1-11和13运行 因此,当我试图通过计算系列的数量并删除索引为1的系列来删除它们时:Act
For Each Series In ActiveSheet.ChartObjects("Chart 4")
'For Each FullSeriesCollection In ActiveSheet.ChartObjects("Chart 4")
ActiveChart.FullSeriesCollection.Delete
Next
我得到一个错误声明:
“对象不支持此属性或方法”
我在stackoverflow上查找了这个问题,发现计数器需要下降:
我复制并调整了以下内容:
这不会删除所有数据系列,因为之后它仍然显示: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存储索引的方式缺乏了解,这导致我尝试了不正确的解决方案
删除if语句后,它将工作
With ActiveChart
If .SeriesCollection.count >0 then
For iSrs = .SeriesCollection.count To 1 Step -1
'If InStr(LCase$(.SeriesCollection(iSrs).name), "series") > 0 Then
.SeriesCollection(iSrs).Delete
'End If
Next
end if
End With
每个方法的
上出现运行时483错误-因为使用此方法意味着您从第一个循环到最后一个。删除对象时,您需要向后循环。因此,为此,您需要使用For iSrs=.SeriesCollection.count到1步-1
尝试下面的代码,代码内部的解释(作为注释):
因此,将代码的最后一部分替换为:
With ChtObj.Chart ' <-- there's no need to select the Chart, use fullay qualified objects instead
If .SeriesCollection.Count >= 0 Then
For iSrs = .SeriesCollection.Count To 1 Step -1 ' allways loop backwards when deleting objects
.SeriesCollection(iSrs).Delete
Next iSrs
End If
End With
使用ChtObj.Chart“#3”——仅当序列名称包含子字符串“序列时,才删除序列“
@a.t.您试过下面的答案吗?有什么反馈吗?谢谢Shai Rado,它让我度过了一个美好的夜晚,它的工作方式很有魅力!我将研究为什么您的否定步骤有效,为什么我的否定步骤无效,这是否是我的编程/键入错误,或者代码的实际方法是否存在差异。感谢您通过首先标注完全限定对象的尺寸来显示ettiquette的改进。最重要的是要有一个手写的、有效的解决方案!我打算给你一个确切的解释。二,。在我自己提出这个问题时,一旦我认为我完全理解了它。对于.SeriesCollection中的每个srs
不关心它的未来,因为它不使用索引,而是使用集合。谢谢!确实如此,我认为Shai Rado的答案更完整,所以我接受了这个答案。然而,在我看来,你的回答同样合格。我感谢你的贡献和努力!
Option Explicit
Sub DeleteChartSer()
Dim Sht As Worksheet
Dim ChtObj As ChartObject
Dim Ser As Series
Dim iSrs As Long
' set the worksheet object (this will work only if "Nutrition planner v42.xlsm" is open)
Set Sht = Workbooks("Nutrition planner v42.xlsm").Worksheets("day_vita_visual")
' set the ChartObject
Set ChtObj = Sht.ChartObjects("Chart 4")
MsgBox ChtObj.Chart.SeriesCollection.Count
With ChtObj.Chart ' <-- there's no need to select the Chart, use fullay qualified objects instead
If .SeriesCollection.Count >= 0 Then
For iSrs = .SeriesCollection.Count To 1 Step -1 ' allways loop backwards when deleting objects
If LCase(.SeriesCollection(iSrs).Name) Like "*series*" Then
.SeriesCollection(iSrs).Delete
End If
Next iSrs
End If
End With
'MsgBox (count_non_existant_series & " and the nr of series still present = " & ActiveSheet.ChartObjects("Chart 4").Chart.SeriesCollection.Count)
End Sub
If LCase(.SeriesCollection(iSrs).Name) Like "*series*" Then
With ChtObj.Chart ' <-- there's no need to select the Chart, use fullay qualified objects instead
If .SeriesCollection.Count >= 0 Then
For iSrs = .SeriesCollection.Count To 1 Step -1 ' allways loop backwards when deleting objects
.SeriesCollection(iSrs).Delete
Next iSrs
End If
End With
Do While ActiveChart.SeriesCollection.Count > 0
ActiveChart.SeriesCollection(1).Delete
Loop