Overflow Query

2016LM

New Member
Joined
Sep 9, 2016
Messages
18
Hi,

I was hoping someone might be able to explain this to me. When I execute the following code (which simply calculates a value, and using debug.print, outputs the result), I get a Run-time Error '6' Overflow message.

Sub calc_ulation()
Dim x As Long
x = 1000 * 60
Debug.Print x
End Sub


However, if I replace the 1000 with either (10^3) or 1000.0 (which is abbreviated to 1000#) I get no error message and the code executes with the output being displayed in the Immediate window. I'm a bit confused by this; 60000 is within the range of a Long variable type.

Any help / explanation would be much appreciated.

Kind regards, 2016LM
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA treats your two literal values as Integers, since they will both fit. It therefore attempts to store the intermediate result of the calculation in an Integer before converting to a Long, but that result is too large, hence the overflow. Using a type declaration character like # or & gets around the issue.
 
Last edited:
Upvote 0
Ah right, that makes sense, many thanks RoryA. And thanks for the quick reply, I really appreciate it; and apologies for my tardy one.

Kind regards, 2016LM
 
Last edited:
Upvote 0
Well, I don't know that it makes sense, but that's what it does. :)
 
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,702
Members
453,748
Latest member
akhtarf3

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