VBA randomly crashes excel

Fedmahn Kassad

New Member
Joined
Sep 8, 2015
Messages
8
Hi everyone,

I have some VBA code that I wrote in order to calculate what is effectively a payback period. I have a threshold ("Reserve"), and use the series of cash flows ("NWO") to figure out how many periods it takes to breach that threshold.

Iterating this formula through thousands of cells in many different sheets causes 2 issues:
1. If I have iterative calculation turned on, the formula won't stop calculating (though I can click stuff as it calculates; it just never stops recalculating)
2. Excel eventually, but inevitably, crashes (always on something like a worksheet rename or entering any formula)

Does anyone have insight as to what may cause this behavior? The second is critical as it prevents the wider use of this code.

Thanks

Code:
Function FWC(Reserve As Double, NWO As Range) As Double
Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual
Dim n As Integer
Dim p As Double
p = WorksheetFunction.Sum(NWO.Columns(1)): n = 1
On Error GoTo FuncFail
Do While p < Reserve
    n = n + 1: p = p + WorksheetFunction.Sum(NWO.Columns(n))
Loop
FWC = (n) - ((p - Reserve) / NWO.Columns(n))
Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic
Exit Function
FuncFail: FWC = CVErr(xlErrNA)
End Function

Reserve is always 1 cell, NWO is always a 20x1 array, both are formula-driven numbers in the millions (ie, 1,250,000). The function gives the CORRECT result, it's the side effects I would like a second opinion on.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
n as Integer?
Shouldn't that be Double too?

I chose Integer as n will only go as high as 20. Single would be too stringent a parameter and Double too large, in my understanding. Let me know if I'm wrong however I don't think this contributes to the problem (again, let me know if I'm wrong!!).

Thanks
 
Upvote 0
Code:
Function FWC(Reserve As Double, NWO As Range) As Variant
  Dim i             As Long
  Dim p             As Double
  Dim av            As Variant

  av = NWO.Value2

  With WorksheetFunction
    For i = 1 To NWO.Columns.Count
      p = p + .Sum(.Index(av, 0, i))
      If p >= Reserve Then Exit For
    Next i
  End With

  If p < Reserve Then
    FWC = CVErr(xlErrNA)
  Else
    [COLOR="#FF0000"]FWC = (i) - ((p - Reserve) / NWO.Columns(i))  ' ??[/COLOR]
  End If
End Function

The red line makes no sense, though. What is the calculation supposed to do?
 
Upvote 0
Code:
Function FWC(Reserve As Double, NWO As Range) As Variant
  Dim i             As Long
  Dim p             As Double
  Dim av            As Variant

  av = NWO.Value2

  With WorksheetFunction
    For i = 1 To NWO.Columns.Count
      p = p + .Sum(.Index(av, 0, i))
      If p >= Reserve Then Exit For
    Next i
  End With

  If p < Reserve Then
    FWC = CVErr(xlErrNA)
  Else
    [COLOR=#FF0000]FWC = (i) - ((p - Reserve) / NWO.Columns(i))  ' ??[/COLOR]
  End If
End Function

The red line makes no sense, though. What is the calculation supposed to do?

I'm not quite sure but it seems you rewrote the code in a potentially better way? Please let me know if that is the case.

The formula inputs, Reserve and NWO, are a 1 cell and a 20 columns x 1 row array. Both are numbers in the 1,000,000's. I need to find A. the number of columns before Reserve < NWO, then the fraction of the next column that, when summed, will make Reserve = NWO. This should yield a number such as "7.29" or "11.4" etc. where the 7 and 11 are the sum of columns where Reserve < NWO, and the .29 and .4 are the fraction of the next column. Does this make sense? Again, this is a payback period calculator for a series of large spreadsheets.

Edit:

I tried your code and it does yield the same result. Thanks. Do you mind pointing out perhaps what the improvements are?

I'm unable to recommend anyone use this code because it causes Excel to crash at random, as well as the slowdown. This new code does not seem to improve the slowdown but if it prevents any crashes, that brings me 1 step closer to the goal. Thanks for your help!!!
 
Last edited:
Upvote 0
Ah. In that case,

Code:
Function FWC(Reserve As Double, NWO As Range) As Variant
  Dim iCol          As Long
  Dim dSum          As Double
  Dim av            As Variant

  av = NWO.Value2

  For iCol = 1 To NWO.Columns.Count
    dSum = dSum + av(1, iCol)
    If dSum >= Reserve Then Exit For
  Next iCol

  If dSum < Reserve Then
    FWC = CVErr(xlErrNA)
  Else
    FWC = iCol - (dSum - Reserve) / av(1, iCol)
  End If
End Function

For example,

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][td="bgcolor:#C0C0C0"]
D​
[/td][td="bgcolor:#C0C0C0"]
E​
[/td][td="bgcolor:#C0C0C0"]
F​
[/td][td="bgcolor:#C0C0C0"]
G​
[/td][td="bgcolor:#C0C0C0"]
H​
[/td][td="bgcolor:#C0C0C0"]
I​
[/td][td="bgcolor:#C0C0C0"]
J​
[/td][td="bgcolor:#C0C0C0"]
K​
[/td][td="bgcolor:#C0C0C0"]
L​
[/td][td="bgcolor:#C0C0C0"]
M​
[/td][td="bgcolor:#C0C0C0"]
N​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td="bgcolor:#F3F3F3"]
NWO(1)
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
NWO(n)
[/td][td="bgcolor:#F3F3F3"]
[/td][td="bgcolor:#F3F3F3"]
Reserve
[/td][td="bgcolor:#F3F3F3"]
FWC
[/td][td="bgcolor:#F3F3F3"]
[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]
343,713​
[/td][td]
87,655​
[/td][td]
58,881​
[/td][td]
132,020​
[/td][td]
32,689​
[/td][td]
384,533​
[/td][td]
298,418​
[/td][td]
486,163​
[/td][td]
100,231​
[/td][td]
110,907​
[/td][td][/td][td]
1,216,531​
[/td][td]
6.593​
[/td][td]M2: =FWC(L2, A2:J2)[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
83,798​
[/td][td]
256,569​
[/td][td]
248,973​
[/td][td]
145,959​
[/td][td]
469,079​
[/td][td]
160,946​
[/td][td]
140,131​
[/td][td]
169,542​
[/td][td]
218,100​
[/td][td]
478,266​
[/td][td][/td][td]
334,868​
[/td][td]
1.979​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]
185,761​
[/td][td]
185,619​
[/td][td]
335,995​
[/td][td]
146,916​
[/td][td]
281,315​
[/td][td]
54,687​
[/td][td]
28,824​
[/td][td]
220,676​
[/td][td]
187,560​
[/td][td]
422,640​
[/td][td][/td][td]
222,081​
[/td][td]
1.196​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
5​
[/td][td]
442,935​
[/td][td]
320,930​
[/td][td]
10,095​
[/td][td]
86,647​
[/td][td]
67,684​
[/td][td]
444,108​
[/td][td]
111,184​
[/td][td]
450,792​
[/td][td]
254,448​
[/td][td]
302,685​
[/td][td][/td][td]
372,099​
[/td][td]
0.840​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
6​
[/td][td]
133,673​
[/td][td]
357,714​
[/td][td]
400,832​
[/td][td]
215,405​
[/td][td]
90,249​
[/td][td]
28,508​
[/td][td]
467,735​
[/td][td]
75,733​
[/td][td]
104,405​
[/td][td]
492,385​
[/td][td][/td][td]
1,443,445​
[/td][td]
6.464​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
7​
[/td][td]
331,054​
[/td][td]
163,679​
[/td][td]
103,257​
[/td][td]
293,846​
[/td][td]
246,634​
[/td][td]
203,809​
[/td][td]
32,789​
[/td][td]
248,219​
[/td][td]
200,840​
[/td][td]
40,934​
[/td][td][/td][td]
439,629​
[/td][td]
1.663​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
8​
[/td][td]
204,821​
[/td][td]
290,567​
[/td][td]
259,441​
[/td][td]
101,754​
[/td][td]
491,733​
[/td][td]
102,552​
[/td][td]
392,050​
[/td][td]
287,950​
[/td][td]
223,407​
[/td][td]
149,277​
[/td][td][/td][td]
858,678​
[/td][td]
4.004​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
9​
[/td][td]
191,675​
[/td][td]
105,241​
[/td][td]
123,693​
[/td][td]
471,502​
[/td][td]
231,432​
[/td][td]
331,070​
[/td][td]
489,560​
[/td][td]
17,969​
[/td][td]
37,257​
[/td][td]
264,822​
[/td][td][/td][td]
1,320,462​
[/td][td]
5.595​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
10​
[/td][td]
206,620​
[/td][td]
241,144​
[/td][td]
163,590​
[/td][td]
365,891​
[/td][td]
259,785​
[/td][td]
239,636​
[/td][td]
103,689​
[/td][td]
80,902​
[/td][td]
59,471​
[/td][td]
104,099​
[/td][td][/td][td]
608,816​
[/td][td]
2.984​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
11​
[/td][td]
472,893​
[/td][td]
350,350​
[/td][td]
300,282​
[/td][td]
115,284​
[/td][td]
265,143​
[/td][td]
313,284​
[/td][td]
275,232​
[/td][td]
498,095​
[/td][td]
352,941​
[/td][td]
209,082​
[/td][td][/td][td]
1,286,471​
[/td][td]
4.180​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
12​
[/td][td]
275,395​
[/td][td]
158,428​
[/td][td]
81,696​
[/td][td]
223,366​
[/td][td]
79,718​
[/td][td]
410,604​
[/td][td]
209,329​
[/td][td]
396,623​
[/td][td]
360,474​
[/td][td]
226,845​
[/td][td][/td][td]
2,422,478​
[/td][td]
10.000​
[/td][td][/td][/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top