+ Reply to Thread
Results 1 to 10 of 10

Overflow Error 6 with Long variable type

  1. #1
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Overflow Error 6 with Long variable type

    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

  2. #2
    Forum Guru
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Overflow Error 6 with Long variable type

    If x = 2000 * 30 refers to a column, you can have a max of 16384 limit in excel 2010.

  3. #3
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Overflow Error 6 with Long variable type

    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.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    21,036

    Re: Overflow Error 6 with Long variable type

    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

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Overflow Error 6 with Long variable type

    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

  6. #6
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Overflow Error 6 with Long variable type

    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

  7. #7
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Overflow Error 6 with Long variable type

    Thanks Joseph...yes these doesnt seem particularly intuitive,...in a general way but you've explained it very well thanks

  8. #8
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Overflow Error 6 with Long variable type

    Quote Originally Posted by JosephP View Post
    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.
    Thanks Joseph...yes this doesnt seem particularly intuitive,...in a general way but you've explained it very well thanks

  9. #9
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Overflow Error 6 with Long variable type

    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!

  10. #10
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Overflow Error 6 with Long variable type

    Quote Originally Posted by Sean Thomas View Post
    I cant explain that one,
    but this works

    Please Login or Register  to view this content.
    Thanks Sean, I think the previous posts explain this quite adequately and I appreciate all the quick responses here....thanks again to all

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1