Search code examples
excelvbamemorydll

Excel crash when looping over large range to update cells using a function call


I have a loop to update cells using a visual studio dll. This works, but excel crashes when I loop over larger (200 cell) ranges. The dll function is evaluated after looking up two parameters for each distinct cell. The function call is slow but will work for all the parameters supplied for any cells of the range. I can run it for RANGE(N100:W110) and for RANGE(N110:W119) but it crashes when I try RANGE(N100:W119). I'm running with 32bit excel as I need to give it to users with both 32bit and 64bit versions.

Here's my macro:


Sub updatecurves()

Dim curvesheet As Worksheet
Dim My_setcell, c As Range
Dim My_invua, My_uascale, My_uyscale, My_beta, My_sigma, My_KD, My_zD, My_nint, My_nstehfest, My_flag
Dim Neuman72
Dim MyMsg
Set curvesheet = Sheets("neuman72 vs Aq")

Application.ScreenUpdating = False

My_uascale = Application.Evaluate(Names("uascale").Value)
My_uyscale = Application.Evaluate(Names("uyscale").Value)
My_sigma = 0.0001
My_KD = Application.Evaluate(Names("KD").Value)
My_zD = Application.Evaluate(Names("zD").Value)
My_nint = Application.Evaluate(Names("testnint").Value)
My_nstehfest = Application.Evaluate(Names("nstehfest").Value)
My_flag = Application.Evaluate(Names("flag").Value)



For Each c In curvesheet.Range("N100:W110").Cells
 
 My_invua = Cells(c.Row, "A").Value
 My_beta = Cells("2", c.Column)
 Neuman72 = Neuman72ForEXL(My_invua, My_uascale, My_uyscale, My_beta, My_sigma, My_KD, My_zD, My_nint, My_nstehfest, My_flag) + expint(1 / (4 * My_invua))
 c.Value = Neuman72
 
Next

Application.ScreenUpdating = True

' some comments

End Sub


Solution

  • OK - Thank you @foxfire-and-burns-and-burns . I was convinced that the problem was with excel and they pointed out that I should look at the cpp-compiled dll that I was calling from VBA as neuman72ForEXL(...) in the VBA of my question. It was a memory leak in a sub function that got called very many (>billion) times and eventually used up available resources and crashed: ''' v = new double[n]; // allocate the array length n ''' needed ''' delete[] v; // free array at end to prevent memory leak ''' Always need to be skeptical of my own coding and remember that the Excel team are less likely to distribute bugs!