Run Time Error 6 - Overflow

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
I am getting a run time error 6 when the following routine runs. I can’t understand why because 330 * 100 = 33,000 and the range of a long is -2,147,483,648 to 2,147,483,647.</SPAN>

Does anyone know what is happening here? Thanks.</SPAN>

Code:
Sub foo()
    Dim num As Long
    num = 330 * 100
    Debug.Print num
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
330 and 100 are Integers by default. So the calculation is being performed on an integer basis (-32,768 to 32,767). 33000 > 32767, so overflow.

The way round it is: num = clng(330) * 100. The calculation will then be done on a Long basis.

Sorry, I'm not very good at explaining these kind of things...but that should be the gist of it :)
 
Upvote 0
That makes perfect sense, thank you. If I declare two other variables as longs, set their values to 300 and 100, use them in the formula instead, it works.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,203,456
Messages
6,055,543
Members
444,794
Latest member
HSAL

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