VBA Overflow Issue

danbrawl93

New Member
Joined
Dec 15, 2016
Messages
17
Hey guys,

I'm having a real issue with some code that isn't working!
Code:
  For c = r_start To r_end
           
            If z <> 7 Then
            value = Evaluate("=SumProduct((A7:A" & z - 1 & " = A" & c & ")*(Numbervalue(" & column_letter & "7:" & column_letter & z - 1 & ")))")
            Else: value = 0
            End If
            
            If x <> (n + 1) Then
            value_2 = Evaluate("=SumProduct((A" & n + 1 & ":A" & x - 1 & " = A" & c & ")*(Numbervalue(" & column_letter & "" & n + 1 & ":" & column_letter & "" & x - 1 & ")))")
            Else: value_2 = 0
            End If
 
            Cells(c, 2).value = value + value_2
 Next c
The variables "value" & "value_2" are set to be integers. However, I seem to keep getting an overflow error for no obvious reason at certain points (e.g. when value = 20000 & value_2 = 15000). I know this is a vague question, but does anyone have any idea what the problem is at this stage? All the variables are correct in terms of the value which they should be.

Thanks so much!
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Which line actually causes the error?
 
Upvote 0
Sorry, my question was poorly written - it would cause an error at seemingly random points depending on cell values. I figured it would be easier to try for a quick fix instead of probing into the specific code line errors, since this error causing line also seemed to vary as the loop proceeded. Anyway...

Thank you so much Derek, your solution worked! Could somebody please hazard a guess as to why "Dim as Long" had such an effect? :)
 
Upvote 0
The greatest value that can be stored in an Integer is 32,767.
Value + Value_2 would exceed that in the example that you gave.
I prefer to use Long where calculation is involved to prevent possible problems when Excel perform intermediate calculations.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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