+ Reply to Thread
Results 1 to 10 of 10

Run-time error 6 overflow

  1. #1
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Run-time error 6 overflow

    I have this code and giving me this error "Run-Time error 6, overflow"

    here is the line of code highlighting the error:

    Please Login or Register  to view this content.

    Complete code:

    Please Login or Register  to view this content.
    thank you,

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Run-time error 6 overflow

    The error message itself has described what is actually happened : overflow, the variable type cannot hold the number to be assigned.

    You describe i as a long datatype :
    Please Login or Register  to view this content.
    and if in VBE editor you select/highlight the Long word and then press F1, the help window will show this :
    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647

    So maximum value the variable i can hold here is 2,147,483,647, or (2 ^ 31) - 1, which mean UBound(aToUse) is greater than 31
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: Run-time error 6 overflow

    First question is the size of Range("rngList") I suspect it's too large. You are trying to generate Long Interer as 2^this_size
    So I think 31 is the limit.
    Best Regards,

    Kaper

  4. #4
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Run-time error 6 overflow

    thank you.

    is there anyway to increase it to 100, sorry no that good in VBA (file attached)
    Attached Files Attached Files

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Run-time error 6 overflow

    If the question is, can we use a trick to accomplish this, the answer is : Yes, I'll show you how :

    Insert a new module and paste this code :

    Please Login or Register  to view this content.
    If we run the sub Test_Long, then there is no problem at all, because variable i is defined as long, and long datatype can hold number 1000.

    But if we run sub Test_Byte_A, the overflow error will be shown up just as your case, because b1 is defined as byte (with its range is between 0 and 255), so b1 here cannot hold number 1000

    Now if we run sub Test_Byte_B, we use double loop, we divide 1000 into two parts : 0 to 4 (total = 5) and 1 to 200 (total =200), so grand total = 5 * 200 = 1000, and the result of this sub Test_Byte_B will be same as sub Test_Long, so this technique can be used to overcome the problem.

    =================================================================================================================================

    But here comes the real problem : ***IT IS VERY SLOW*** if you want to solve the problem with big data using your current approach.

    Let me give an example, on your MBinary module (the one where Sub SumToTarget() is located), add this code (which its code is just a small part of Sub SumToTarget) :
    Please Login or Register  to view this content.
    You can see, we only try to use 20 cells only --> (2 ^ 20)
    The result of the code on my PC is :
    Start at : 20:49:06
    Stop at : 20:49:29

    or in other word, it takes about 25 seconds for 20 cells (--> 2 ^ 20)


    Now let's say we want to calculate using your data on sheet "FIND_TARGET", which has 75 cells of data.
    So roughly, we can guess the time needed (compared to our sub Test_Time result) will be :
    = 25 seconds * (2 ^ (75 - 20))
    = 9.00719925474099E+17 seconds
    = 9.00719925474099E+17 / (60 * 60 *24) days
    = 10,424,999,137,432 days

    You see, this is just a nonsense, just don't do it using this approach.

    =================================================================================================================================

    So now, how to solve the problem ?
    Using VBA alone (which is just a scripting language) is just too slow, we must find another approach.
    I see many people here using Excel built in Solver, or third party addin OpenSolver to solve this kind of problem.
    As a compiled code, Solver works very very much faster compared to VBA.
    We here in ExcelForum have some very talented Experts who are mastering in this field of Solver.
    So my suggestion is, you open a new thread which expect using solver to solve your problem, the title would be something like :
    "How to use solver to achieve specific Target Sum", and hopefully our experts will be shown up to help you with your case.



    Regards

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Run-time error 6 overflow

    What is it you actually are wanting to achieve here...
    What is expected output required?
    Extract all figures that add up to 25865.47?
    Last edited by sintek; 12-19-2020 at 11:03 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  7. #7
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Run-time error 6 overflow

    Thank you.

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Run-time error 6 overflow

    Thank you.
    Response to Post 6?

  9. #9
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Run-time error 6 overflow

    YSorry, yes this:

    Extract all figures that add up to 25865.47?

    Thanks again

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Run-time error 6 overflow

    Extract all figures that add up to 25865.47?
    This will give you an idea...Can be amended for other output...
    All figures adding up to ..... will be highlighted in red....
    Please Login or Register  to view this content.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Run-time error '6': Overflow
    By LearningSlow in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-03-2020, 05:04 PM
  2. Run-time error 6 overflow
    By Nathanpires in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-27-2020, 05:04 AM
  3. [SOLVED] Run Time Error 6 - Overflow
    By danbates in forum Excel General
    Replies: 3
    Last Post: 08-21-2017, 10:35 AM
  4. Run-time error '6' overflow
    By tranxcop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2015, 09:00 PM
  5. [SOLVED] Run-time error '6': Overflow
    By msawyer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-14-2012, 01:29 PM
  6. Run-time error '6': Overflow
    By johnlynches in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-02-2012, 12:07 PM
  7. Run-time error 6 : overflow
    By Mbrolass in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2007, 03:18 PM

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