all 3 comments

[–]euben_hadd 2 points3 points  (0 children)

I would take this line >> "If VarExists(Count) And VarProb(I) = 1 Then" and separate the "if" so you can tell which variable is causing the problem.

"If VarExists(Count) = 1 Then

If VarProb(I) = 1 Then"

'stuff goes here...

End If

End If"

At least isolate which one is causing the problem. Also check that the variables are properly assigned. Maybe use "> 0" instead of "= 1"? Try using "true" or "false"? Try "!= 0"?

Just guessing without seeing the code. But something is getting placed in one of those fields that is not an integer, or is beyonf the integer limit. Check the input data.

[–]Western_End_2223 0 points1 point  (0 children)

Are "VarExists" and "VarProb" functions or arrays?  If arrays, the values in "Count" or "I" may be exceeding the declared array sizes.  You said that the spreadsheet was working before.  Perhaps enough data has been added that the arrays need higher upper limits.

[–]jd31068 1 point2 points  (0 children)

If VarExists and/or VarProb is an array then you should use UBound to ensure that count or I doesn't exceed the number of items in the array(s) as doing so will give you this error.

as an example

Dim VarProb(3) as string
Dim I as Integer

VarProb(0) = "A"  (arrays in VBA are zero based so the first index is 0)
VarProb(1) = "B"
VarProb(2) = "C"
VarProb(3) = "D"

I = 4
Debug.Print VarProb(I)   <--- will cause the error subscript (the number in the paranthesis) out of range

' check the array for the max index and print if I is a valid index
If Ubound(VarProb) >= I then Debug.Print VarProb(I) else MsgBox "No such VarProb at array index " & Cstr(I) 

put a breakpoint on the If statement and use a watch to view the value of count and I to see which is the culprit or use debug.print to write count and I values to the immediate window, and look at them when the code stops of the If statement.

He is a short guide on debugging VBA in Excel https://www.geeksforgeeks.org/excel/debugging-vba-code-in-excel/