我刚刚花了很长时间来解决这个问题。不管我做什么,该死的 excel 从某个地方获取了一些数据,这真的很烦人。.ChartArea.Clear 方法在我之后操作图表时导致崩溃(甚至填充了新系列)
这是我从一个真正的工作项目中得到的最终解决方案(一个片段,对一些用户特定的变量感到抱歉)
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
...