+ Reply to Thread
Results 1 to 11 of 11

VBA debug: False "Capacity overrun" error

  1. #1
    Registered User
    Join Date
    04-30-2024
    Location
    France
    MS-Off Ver
    2010
    Posts
    6

    VBA debug: False "Capacity overrun" error

    Hello,
    In the execution of a VBA code a "Capacity overrun" error occurs on my "CNm" variable type Long at the following step of the code:
    Attachment 867685

    We can see in the variables window values that Nm=183. Making the operation by hand doesn't give a CNm result over the Long capacity.D?passement de capacit? CNm.jpg

    Does someone have an idea about the problem cause?
    I see only hacking. Are you agree?

    Thanks by advance.
    With best regards,

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: VBA debug: False "Capacity overrun" error

    Declare Nm as Long, not Integer or specify your first literal value as a Long like this:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: VBA debug: False "Capacity overrun" error

    It's vba quirk. Math operator (*, + etc), when supplied with INT value will return INT and will cause this error.

    Try something like...
    Please Login or Register  to view this content.
    By using CLng on one of the int value, it will force calculation to return long type and avoid that error.

    EDIT: You can test this by doing simple math operation CNm = 183 * 183 will generate Overflow error. But you can use CNm = CLng(183) * 183 to return result without error. Also note division and subtraction won't cause this error. Go figure.
    Last edited by CK76; 04-30-2024 at 12:45 PM.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  4. #4
    Registered User
    Join Date
    04-30-2024
    Location
    France
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA debug: False "Capacity overrun" error

    Thanks for the answers. Both are good solutions but I don't understand why does the error occur only at Nm=183!!
    It means that my Integers are coded on 15bits only and not 2bytes as they should be!!
    In fact, the error occurs at Nm=183 when CNm should be 16471 and goes over the signed values on 15bits (16384). With Nm=182, the program gives CNm=16290 without error.
    As positive integer values are coded on 16bits (2bytes) they go up to 32768 and the error should occurs at Nm=258.
    Can you confirm that the actual error shows an abnormal range for an Integer value?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: VBA debug: False "Capacity overrun" error

    The first part of your expression evaluates to 181 * 182 which is 32942 - more than an integer value.

  6. #6
    Registered User
    Join Date
    04-30-2024
    Location
    France
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA debug: False "Capacity overrun" error

    Ok! Thank you!
    But I have checked with the following code:
    Dim Nm As Long
    Nm=181*182
    and the Capacity overrun occurs on Nm calculation!... Any suggestion?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: VBA debug: False "Capacity overrun" error

    It's the same problem. Use:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    04-30-2024
    Location
    France
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA debug: False "Capacity overrun" error

    Sorry, but it is not the same problem because I shouldn't have to translate the format (with "&") to make the result of the operation fit with a Long format. In fact, the size of the result is in the range of a Long variable. Why do I have to translate the operation?

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: VBA debug: False "Capacity overrun" error

    Like I stated previously in post #3. INT * INT or INT + INT that result in Long type will generate that error. As VBA tries to return INT type rather than Long type.
    What you set the variable that will hold the result have no relevance here. It's the data type of number being applied operator that will cause this.

    It's VBA quirk. It is the same underlying issue.

  10. #10
    Registered User
    Join Date
    04-30-2024
    Location
    France
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA debug: False "Capacity overrun" error

    I didn't precise that my code works with all others numeric values of Nm<183... it is well a problem of capacity range when Nm=183. So, in my case INT * INT or INT + INT do not always make an error. Why don't I have an error with smaller Nm values?
    I understand that the result of the operation with Integers must not rise above 32767 whatever the storage variable type. But now, I don't understand why the code hereunder makes a Capacity overrun error:
    '
    Dim Nm As Long
    Nm=181*182
    '

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,966

    Re: VBA debug: False "Capacity overrun" error

    Because there is an implicit variable of type Integer that holds the result(s) of the expression on the right hand side before that is assigned to your variable. (In other words, yes it is the same problem )

+ 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] Renaming File from "Scan.pdf" to Target Cell Text, Debug Error.
    By thenewkidd in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-14-2023, 11:00 AM
  2. [SOLVED] Why I get "Object required" debug error (Beginner level)
    By rcurious in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2021, 02:23 AM
  3. Replies: 11
    Last Post: 08-05-2020, 10:10 AM
  4. Excel 2010 (Run-time error '13' type mismatch) "Debug" and "Continue" Grayed out.
    By Jeronimo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2012, 06:42 PM
  5. Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" not working
    By redders in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2011, 03:52 PM
  6. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  7. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

Tags for this Thread

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