user3903098
user3903098

Reputation: 163

Initializing chart object in crashes vba

I'm currently writing a program that will create several charts by using a dataset. Every time I want to make a new chart, I call on a function.

However when I go to initialize the chart object in that function, VBA just crashes and I have to terminate the program.

Any idea why? I've tried initializing the chart object in my methods and it's the same result.

Here's the code (as soon as it hits "Set chartSheet = Charts. Add", vba stops responding):

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

EDIT

I ended up fixing the problem by initializing the chart like this:

ActiveSheet.Shapes.AddChart.Select Set chartSheet = ActiveChart

Still don't understand why what I initially wrote would cause vba to crash

EDIT2

I jinxed myself, this new method now crashes vba too. It somehow worked once though, so I'm starting to wonder if it has to do with my computer

Upvotes: 0

Views: 1286

Answers (1)

user3903098
user3903098

Reputation: 163

Ok, so I think I understood what was happening.

The worksheet I was working with is really big (>60k rows), so when I'd run the vba code, if a cell happened to be selected in the current region (i.e. on a non-blank cell) initializing a chart would try and plot some 60k data-point monstrosity.

I fixed my issue by making sure that I was selecting a blank cell before I ran the code.

Upvotes: 2

Related Questions