Can anyone explain why, if variable type long can hold numbers up to 2,147,483,647, this code fails with Overflow (Error 6):
Dim x As Long
x = 2000 * 30
Thanks![]()
Can anyone explain why, if variable type long can hold numbers up to 2,147,483,647, this code fails with Overflow (Error 6):
Dim x As Long
x = 2000 * 30
Thanks![]()
If x = 2000 * 30 refers to a column, you can have a max of 16384 limit in excel 2010.
No assumptions have been made in the code about columns. The code appears to fail "as is" regardless of any spreadsheet assocations (there weren't any)...thanks for trying to help. In fact there are frequent references to the same MS posting that says that a similar couple of lines of code will fail along with a fix. However, it's not clear to me why this would fail
Dim x As Long
x = 2000 * 365 ' Error: Overflow
To work around this situation, type the number, like this:
Dim x As Long
x = CLng(2000) * 365
Last edited by ThermalTD; 06-05-2013 at 08:13 AM.
2000 and 30 are both Integer type, so VBA tries to store the result of the calculation into a Integer type before it convey to a Long type variable.
Obviously 2000 * 30 is out of range for Integer, hence error.
try Clng(2000) * 30
I think vba defaults the two literals to integer and then tries to produce an intermediate integer to store the result before assigning to your variable-you an also avoid by using
![]()
Please Login or Register to view this content.
Josie
if at first you don't succeed try doing it the way your wife told you to
Hmmm...hadnt really had to think of numbers in that sense before.....so such numbers are automatically assigned a type even though they are not declared in any sense??? I have experienced a paradigm shift - thanks
Thanks Joseph...yes these doesnt seem particularly intuitive,...in a general way but you've explained it very well thanks
I cant explain that one,
but this works
![]()
Please Login or Register to view this content.
Regards
Sean
Please add to my reputation if you think i helped (click on the star below the post)
Mark threads as "Solved" if you have your answer (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [code]
Please supply a workbook containing example Data: It makes its easier to answer your problem & saves time!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks