+ Reply to Thread
Results 1 to 8 of 8

Stack out of Space - asking too much of VBA?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-29-2007
    Posts
    42

    Stack out of Space - asking too much of VBA?

    Hello again all,

    Long story short, I've got a bit of code which simulates an odometer (mileage meter) by sampling a simulated speed value regularly and adding it to a running total. However, in order to achieve anything like the resolution / accuracy I need, I have to sample ten times a second. Which I appreciate is pretty demanding. I can run it for approximately a minute and a half before I get the Stack out of Space error.

    So, having done a little research it seems like using Do Events in the way I have is a bit of a no-no, however, I've not been able to find an alternative that will give the same small time delays. I have also been trying to find a way to manually clear the Stack periodically in the code, but have again drawn a blank.

    Could one of you possibly have a look over my code and see if there's any housekeeping that can be done that might improve things?

    Would be much appreciated!

    Public Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
    Sub WasteTime(Finish As Long)
     
        Dim NowTick As Long
        Dim EndTick As Long
     
        EndTick = GetTickCount + (Finish)
         
        Do
     
            NowTick = GetTickCount
            DoEvents
     
        Loop Until NowTick >= EndTick
     
    End Sub
    Sub InterOdo()      'calls InterOdoSample sub after 100mS to avoid extra sample at 00:00:00 and waits 100mS between samples (executions of InterOdoSample)
    
    WasteTime (100)
    Call InterOdoSample
    
    End Sub
    Sub InterOdoSample()                    'samples the actual speed and adds it to Total & Intermediate every 100mS
    Dim ForeCheck As Range
    Set ForeCheck = Range("G10")
    Dim FreezeCheck As Range
    Set FreezeCheck = Range("G12")
    
    If FreezeCheck.Value = False Then       'checks for Freeze tick box - should freeze both odometers
    If ForeCheck.Value = True Then          'checks for Reverse tick box - shouldmake both odometers count backwards
    
    Range("E9").Value = Range("E9").Value - (Range("P5").Value / 32850) 'subtacts (compensated) speed sample in meters/0.1sec from total value
    Range("E13").Value = Range("E13").Value - (Range("P5").Value / 32850) 'subtracts (compensated) speed sample in meters/0.1sec from intermediate value
    
    Else
    
    Range("E9").Value = Range("E9").Value + (Range("P5").Value / 32850) 'adds (compensated) speed sample in meters/0.1sec to total value
    Range("E13").Value = Range("E13").Value + (Range("P5").Value / 32850) 'adds (compensated) speed sample in meters/0.1sec to intermediate value
    
    End If
    End If
    
    Call Exercise1                          'starts exercise 1 measurements
    
    If [N5] > [P5] Then                                                                         'if demand is greater than actual (accelerating)
    [P5] = WorksheetFunction.Min([N5], [P5] + WorksheetFunction.Min([N5] - [P5], 6 / [S5]))     'creates a rate-of-change between N5 demand and P5 actual basedon 0-60mph time in S5
    Else
    [P5] = WorksheetFunction.Max([N5], [P5] - WorksheetFunction.Min([P5] - [N5], 6 / [S6]))     'creates a rate-of-change between N5 demand and P5 actual basedon 60-0mph time in S6
    End If
    
    Call InterOdo                           'repeats sampling process at rate set by WasteTime()
    
    End Sub
    The sub Exercise 1 in an another module is:

    Sub Exercise1()                         'code for exercise 1 - achieve 30mph average by 1 mile mark from standing start
    
    If Range("B9") = 1 Then                 'detects total distance = 1.00 miles and copies end time to K14:
    Range("K14").Value = Range("C1").Value
    Call EndExercise1                       'ends exercise
    End If
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Stack out of Space - asking too much of VBA?

    You have set up a serious of loops, that is why you are running out of stack space.

    The Normal way to do this sort of thing is by using the OnTime function

    That way you get the Microsost Operating system to trigger your event as required.
    As there is no loop, you cannot run out of stack space.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,983

    Re: Stack out of Space - asking too much of VBA?

    You cannot manually clear the stack.

    The stack is the space used by each Sub or Function that is loaded. When a Sub calls another Sub, that second Sub is added to the stack. If the second sub calls a third sub, then the third sub is added to the stack, creating a "stack" of three subs. After the third sub exits, it is released from the stack, and so forth.

    You are running out of stack space because InterOdo calls InterOdoSample, putting two Subs in the stack. Then InterOdoSample calls InterOdo. Even though InterOdo is already in the stack, this call create a brand new copy of it, so you have three subs in the stack. This keeps going back and forth between the two until there is no more stack space available to add another sub.

    In InterOdoSample, instead of calling InterOdo, why not just directly call WasteTime?

    Also, your busy loop with DoEvents in WasteTime is going to drain CPU. Instead of WasteTime you should do this:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    ...
    Sleep (100)
    ...
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    01-29-2007
    Posts
    42

    Re: Stack out of Space - asking too much of VBA?

    Thanks for the replies, folks.

    Application.Ontime can't do intervals of less than a full second. Unless I'm wrong?

    I need the worksheet to remain responsive, so using the Sleep Command is out, too. Apologies, should have mentioned this in the original post.
    Last edited by creationracing; 11-03-2018 at 06:47 PM.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,983

    Re: Stack out of Space - asking too much of VBA?

    Yes, OnTime rounds EarliestTime to the nearest second.

    Quote Originally Posted by creationracing View Post
    I need the worksheet to remain responsive, so using the Sleep Command is out, too. Apologies, should have mentioned this in the original post.
    Your wait time is only 0.1 seconds. You need it to remain responsive for that interval? If you DoEvents between Sleeps, then there is still ample opportunity for interaction.

  6. #6
    Registered User
    Join Date
    01-29-2007
    Posts
    42

    Re: Stack out of Space - asking too much of VBA?

    Quote Originally Posted by 6StringJazzer View Post
    Yes, OnTime rounds EarliestTime to the nearest second.

    Your wait time is only 0.1 seconds. You need it to remain responsive for that interval? If you DoEvents between Sleeps, then there is still ample opportunity for interaction.
    The code is looping until a certain value on the odometer is reached, so that 0.1 second WasteTime interval is repeated nose-to-tail, if you like. So, apart from the time taken to run a few lines of code between loops, the code is pretty much always "Wasting Time". So it's important that the method used keeps the sheet active.

    So, I've removed the InterOdo sub and written the WasteTime into the InterOdoSample sub. What's the best way to get it to loop back on itself until B19 = 1.00? A Do Until loop command?

    Thanks again in advance - appreciate the guidance.

  7. #7
    Registered User
    Join Date
    01-29-2007
    Posts
    42

    Re: Stack out of Space - asking too much of VBA?

    Thanks for the explanation on Loops - I'll have a look and try to tidy it up as per your advice.

    Cheers for the advice so far!

  8. #8
    Registered User
    Join Date
    01-29-2007
    Posts
    42

    Re: Stack out of Space - asking too much of VBA?

    Hi folks - I've used a Do Until loop to repeat the required parts of code. All working well and runs for at least 20 mins with no stack overflow errors.

    I'm calling this solved - thanks a million for the advice, you're all superstars.

+ 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. Out of stack space
    By audowl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2017, 01:09 AM
  2. Out of stack space
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2013, 01:06 AM
  3. [SOLVED] Out of Stack Space
    By Phoenix5794 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-29-2012, 08:44 PM
  4. Out of Stack Space
    By process in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-02-2012, 11:31 AM
  5. out of stack space??
    By adds007 in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 02-22-2011, 12:02 PM
  6. Out of stack space
    By papa jonah in forum Excel General
    Replies: 5
    Last Post: 04-13-2005, 06:06 PM
  7. Out Of Stack Space
    By Momo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-03-2005, 11:06 AM

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