+ Reply to Thread
Results 1 to 5 of 5

How to Avoid Infinite Loop?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-04-2012
    Location
    Panama City, Florida
    MS-Off Ver
    Excel 2010
    Posts
    43

    How to Avoid Infinite Loop?

    I'm writing a program that needs to perform a certain function with a "do until" loop, but if the task I want it to "do" is going to result in an infinite loop, I want the program to switch to a different "do until" loop. For example, I want it to perform task "x" and loop until y=0, but there will be some instances where the task in the initial loop will never cause y to equal 0, so if that is the case I want it to perform task "z" and loop until y = 0. The second loop will always eventually lead to y = 0, but how do I get the program to realize the initial loop is infinite/cannot be done if that is the case? I hope this isn't too vague...

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to Avoid Infinite Loop?

    What is the function this program is meant to do?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-04-2012
    Location
    Panama City, Florida
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: How to Avoid Infinite Loop?

    Quote Originally Posted by Norie View Post
    What is the function this program is meant to do?
    I actually figured out what I needed to do, but the first function should only need to run 4 times at most so I just put "variable = variable + 1" in the "do" statement, then have it "loop until variable = 4". Thanks anyway!

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: How to Avoid Infinite Loop?

    Hi,
    One way to do it would be to put a counter in your first loop. If that counter reaches a determined value the change loop. There may be some other / better way to do it, but without having more details, it's difficult to say.

    n = 0
    Do Until Y = 0
       Perform_Task_X
       n = n + 1
       If n > 10000 Then GoTo Z:
    Loop
    
    Z:
    Do Until Y = 0
       Perform_other_task
    Loop
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  5. #5
    Registered User
    Join Date
    10-04-2012
    Location
    Panama City, Florida
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: How to Avoid Infinite Loop?

    Quote Originally Posted by GC Excel View Post
    Hi,
    One way to do it would be to put a counter in your first loop. If that counter reaches a determined value the change loop. There may be some other / better way to do it, but without having more details, it's difficult to say.

    n = 0
    Do Until Y = 0
       Perform_Task_X
       n = n + 1
       If n > 10000 Then GoTo Z:
    Loop
    
    Z:
    Do Until Y = 0
       Perform_other_task
    Loop
    Just saw this. Thanks!

+ 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. Why is my loop infinite, and how can I fix it?
    By Mrowe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-13-2012, 02:48 PM
  2. [SOLVED] VBA infinite loop
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-13-2012, 12:20 PM
  3. how to avoid infinite loops ine excel vba ?
    By meen2525 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-26-2011, 07:46 AM
  4. Infinite Loop
    By phong919 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-22-2010, 04:56 PM
  5. For Next Infinite Loop
    By Naji in forum Excel General
    Replies: 5
    Last Post: 01-13-2006, 03:00 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