我目前正在编写一个程序,该程序将使用数据集创建多个图表。每次我想制作一个新的图表时,我都会调用一个函数。
然而,当我在该函数中初始化图表对象时,VBA就会崩溃,我不得不终止程序。
知道为什么吗?我已经尝试在我的方法中初始化图表对象,结果是一样的。
下面是代码(只要点击"Set chartSheet=Charts.Add",vba就会停止响应):
Function saccadeGraph(startRow As Long, sncol As Integer, sacol As Integer, iacol As Integer)
Dim chartSheet As Chart
Set chartSheet = Charts.Add
Dim lastRow As Long
lastRow = startRow
''Finds row where the slide ends
While Cells(lastRow, sncol) = Cells(lastRow + 1, sncol)
lastRow = lastRow + 1
Wend
With chartSheet
.ChartType = xlLineMarkers
.SetSourceData (Range(Cells(startRow, sacol), Cells(lastRow, sacol)))
.SeriesCollection(1).XValues = Range(Cells(startRow, iacol), Cells(lastRow, iacol))
''modify when I'm able to know the index of the target IA
.SeriesCollection(1).Points(3).Select
End With
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
.Solid
End With
saccadeGraph = 0
End Function
编辑
我最终通过如下初始化图表来解决问题:
ActiveSheet.Shapes.AddChart.Select
Set chartSheet = ActiveChart
仍然不明白为什么我最初写的东西会导致vba崩溃
EDIT2
我搞砸了自己,这个新方法现在也崩溃了vba。但不知何故,它只工作过一次,所以我开始怀疑它是否与我的计算机
好吧,所以我想我明白发生了什么。
我使用的工作表非常大(>60k行),所以当我运行vba代码时,如果碰巧在当前区域中选择了一个单元格(即在非空白单元格上),初始化图表会尝试绘制一些60k数据点的怪物。
我通过确保在运行代码之前选择了一个空白单元格来解决问题。