+ Reply to Thread
Results 1 to 12 of 12

Out of stack space error

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Out of stack space error

    I'm running a while loop and after about 3000 iterations get this error:

    "Out of stack space"

    Any idea why this happens/ how to fix it?

    Thanks

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    The stack is where recursive routines store their active variables when recursed, and each recursion pushes another set of data (stack gets deeper) until things unwind and get taken off.

    You have finite space on the stack; if rountines nest too deeply, you run out.

    It's what happens if you stack work in an inbox as received and work only on the top item. If you get too far behind, the stack falls over.

    The way to fix it is to redesign your code.

  3. #3
    Registered User
    Join Date
    08-10-2007
    Posts
    51
    Use the collection object and push things to the collection object or to a hash table.

  4. #4
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    OK, thank you.

  5. #5
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Out of stack space error

    I have a master macro that runs a loop of 8,000 iterations or so.

    Within the loop, if condition 1 is met, function 1 runs (function 1 is written in another module, outside of master), then returns back to master macro; if condition 2 is met, function 2 runs (function 2 is written in another module, outside of master), then returns back to master macro;

    Every time I run this, I get "Out of stack space error" after about 2000 iterations.

    If I put the code for function 1 and 2 within the code of master, and run the master, no error pops up. My prefference is to have master and functions 1 and 2 separate.

    I'm a bit new to programming - what is the language I need to use to elliminate this error?

    Thanks.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Post your code, remember to use Code tags
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    Here it is:


    Master Macro:

    Please Login or Register  to view this content.

    Functions 1 and 2:


    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    Any luck with this?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Sorry but that code appears to be all over the place.

    You seem to be running subs from other subs then calling back to those subs again.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Threads merged. luv2glyd, please don't do that.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Routine A calls B and C
    Routine B calls A
    Routine C calls A
    So the stack gets deeper until it overflows.

    I don't think anyone could help without having a clue as to what you're trying to do, and seeing the six missing routines.

  12. #12
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612
    1. You have not declared and typed your variables, so VB is assigning them the default type of Variant (16 bytes vs 2 for Integer and 4 for Long)
    2. The variable intrade is used in each of the three procedures and it appears that you intended to have the value accessed and changed by each of the three variables; but, since it is undeclared, each procedure initializes a local variable of 16 byte Variant-type with no link to any previous value each time the procedure is called. You should declare it as Public at the top of the module (outside any procedure) and type it as Integer.
    3. You are using "application.run..." to run code in each module, instead, use CALL procedure name.
    4. In your sub procedures, you are trying to exit the procedure and return to the main procedure by using the App...run.. language; however, this will only call the main as a subroutine of the calling subroutine and start it from the beginning of the code. Since main is calling each of the subs, you have an untended infinite loop that crashes Excel. If you had CALLed the sub from the main, the End Sub would exit the sub and return to the main at the next line after the CALL line:

    Please Login or Register  to view this content.

    Declare & Type all variables
    Use Call vs. app/run
    Ben Van Johnson

+ 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