为什么我的Excel图表中暂时缺less一些系列?

我正在从VBmacros生成一个Excel列图。 我添加了3条水平参考线,发现macros完成时只出现第一条(与列图一起)。 但是,如果我保存工作簿并重新打开它,所有3个参考线都可见。 另外,如果我去devise选项卡“select数据”,只需点击任何一个参考线名称,立即出现所有3。 任何想法,我可以把我的macros,让他们都自动出现在macros完成时?

创build这3个参考线的代码是( sc是图表的SeriesCollection ):

 With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "A" .XValues = "={1,3}" .Values = "={100,100}" End With With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "B" .XValues = "={1, 3}" .Values = "={80, 80}" End With With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "C" .XValues = "={1, 3}" .Values = "={50, 50}" End With 

编辑:这是一个截图使用@ axel-richter答案中的数据。 这是在macros结束后立即看起来的样子。

截图

我想这是Excel 2010及更高版本。

添加新的系列后,调用不带参数的Chart.ChartWizard方法 。

例:

假设我们有:

在这里输入图像说明

然后运行这个macros后:

 Sub addChart() Dim oChart As ChartObject Dim sc As SeriesCollection Set oChart = ActiveSheet.ChartObjects.Add(300, 40, 300, 200) oChart.Chart.ChartWizard Source:=ActiveSheet.Range("A1:D4"), Gallery:=xlColumn Set sc = oChart.Chart.SeriesCollection With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "A" .XValues = "={1,3}" .Values = "={100,100}" End With With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "B" .XValues = "={1, 3}" .Values = "={80, 80}" End With With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "C" .XValues = "={1, 3}" .Values = "={50, 50}" End With oChart.Chart.ChartWizard End Sub 

我们有:

在这里输入图像说明

我仍然没有解释为什么这些行不显示为我,但我find了一个“解决scheme”,强迫他们成为知名度。 我在macros的末尾添加了以下内容:(a)在最后添加一个额外的系列,(b)立即删除它。

 With sc.NewSeries .ChartType = xlXYScatterLinesNoMarkers .Name = "D" .XValues = "={1, 3}" .Values = "={50, 50}" End With sc(sc.Count).Select Application.SendKeys "{Delete}" 

注意最后一行。 使用Selection.Delete删除额外的系列,但隐形的行保持这种方式。

如果你有时间,不要重复使用你的SeriesCollection对象。

我遇到了一个C#应用程序,我正在写。 我可以看到我们的代码之间唯一的相似之处是我们都在同一个SeriesCollection对象上调用NewSeries()。

另外值得注意的是Chartwizard,快速系列创build删除解决方法不适合我。

在每个SeriesCollection.NewSeries()调用之前,图表对象上的工作是调用seriescollection()。

如:

 ChartObject co = target.ChartObjects().Item(1); Chart c = co.Chart; SeriesCollection s = c.SeriesCollection(); Series s1 = s.NewSeries(); s1.ChartType = XlChartType.xlLine; s = c.SeriesCollection(); Series s2 = s.NewSeries(); s2.ChartType = XlChartType.xlLine; 

总猜测,但我怀疑在SeriesCollection类的缺陷; 潜在的某些事件不会在随后的NewSeries调用中触发。 根据我们每个人正在使用的解决方法,导致事件不是触发事件,因此,我们会看到隐形序列实际上出现。