如何获取charts.add命令来提供一个空的图表?

VBA中的命令charts.add将新的图表对象添加到工作簿。 我认为在Excel中打F11是完全一样的。

问题是,这个命令使用围绕在那个点select的工作表和单元格的数据填充图表。 如果没有数据或没有可用的数据,它将返回一个空的图表。

我的问题是: “我怎样才能强制命令提交一个空图表?” 。 我打算用VBA代码填充图表。

这个问题的一个简单的答案是创build一个新的空工作表,并select该工作表上的单元格A1,然后创build一个新的图表,但这是一个相当丑陋的解决scheme。 任何帮助优雅的解决scheme,将不胜感激。

尝试添加额外的行,将立即删除图表后出现的数据:

 Charts.Add ActiveChart.ChartArea.Clear 

编辑替代解决scheme,但这将跳回到数据表:

 'select last cell in the sheet which is usually empty Dim tmpSel As Range Set tmpSel = Selection Cells(Rows.Count, Columns.Count).Select 'add chart Charts.Add 'back to base sheet and select range previously selected tmpSel.Parent.Activate tmpSel.Select 

解决这个问题我花了很多时间。 不pipe我做了什么,血腥的excel从某个地方拿走了一些数据,这实在太讨厌了。 .ChartArea.Clear方法导致崩溃,每当我操纵图表后(甚至填补新系列)

这是我从一个真正的工作项目(片段,最后的解决scheme,对于一些用户特定的variables抱歉)

 Dim Chrt as Chart ' This is "the elegant" method ' Parameters of Add do not actually matter, we will specify all details later Set Chrt = MySheet.ChartObjects.Add(0, 0, 100, 100).Chart ' And here we start to fill the empty (!) Chart with some useful series... :) With Chrt ' Specifying chart type .ChartType = xlXYScatterLinesNoMarkers ' Positioning (Page is a range - fragment of my code) With .Parent .Top = Page(3, 1).Top .Left = Page(3, 1).Left .Width = Page.Width .Height = (Page.Height - (Page(4, 1).Top - Page(1, 1).Top)) ' / 2 End With ' Adding a new serie With .SeriesCollection.NewSeries .Name = "D" & Work.Cells(iFileRow, 2) .XValues = Range(MySheet.Cells(2, 1), MySheet.Cells(DataEnd, 1)) .Values = Range(MySheet.Cells(2, 10), MySheet.Cells(DataEnd, 10)) .MarkerStyle = -4142 ' Formating the series With .Format.Line ... 

我遇到过同样的问题。 我尝试使用ActiveChart.ChartArea.Clear属性,并导致崩溃。 然后,我添加图表后,尝试ActiveChart.ChartArea.ClearContents,它给了我想要的结果,这是一个空白的图表,我可以添加系列。

Excel会尝试使用所选数据或包含活动单元格的数据块来填充全新的图表。

在未使用的范围中select一个空白单元格。 没有数据绘图,Excel将插入一个空白图表。

我喜欢ActiveChart.ChartArea.Clear答案。

我一直在做以下事情,当我在Excel 2003中做了一个macros时,它会手动删除所选图表中的所有系列。

 Charts.Add ActiveChart.ChartType = xlXYScatterLinesNoMarkers ActiveChart.Location Where:=xlLocationAsNewSheet ' manually delete every series that may be in a new chart On Error GoTo 30 yy = ActiveChart.SeriesCollection.Count For xx = yy To 1 Step -1 ActiveChart.SeriesCollection(xx).Delete 30 Next xx