+ Reply to Thread
Results 1 to 23 of 23

Loops are driving me loopy! How to exit a loop as soon as condition fails

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Loops are driving me loopy! How to exit a loop as soon as condition fails

    I'm having trouble with conditional loops. I want to test a number of boolean functions and exit the loop immediately after the first fail. (see dummy code below. Even when condition fails on line 50, the code keeps blindly checking all remaining tests in the loop)

    Is there any way to exit loops on condition failure? Short of changing all the functions to
    Please Login or Register  to view this content.
    (That would be as bad as what I had before which was a mass of nested IF ELSEIF. I was trying to change to a DO LOOP to improve readability but I keep hitting this problem)


    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  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

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    You need to test after each assignment:

    Please Login or Register  to view this content.
    Or

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Thanks for the suggestions shg. I guess I am looking for a native method to exit a loop immediately on condition fail *.

    I had been googling before I started this thread and found this ( http://stackoverflow.com/questions/1...oop-in-vbs-vba ) - however the answers don't work or they fall back on test (and Exit Do) after every single function call within the loop (as I excluded in my OP)


    * As opposed to having to test the variable after each function call (and then Exit Do) or abusing On Error. If they were the only options, I'd go back to the nested IF ELSE IFs I was using before!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    I guess I am looking for a native method to exit a loop immediately on condition fail
    There's no such construct as "on false exit do"

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Quote Originally Posted by shg View Post
    There's no such construct as "on false exit do"
    I'll put it another way. Why doesn't DO UNTIL and DO WHILE behave as their names imply? When the condition breaks, why does VBA persist in processing the remainder of the current loop???

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,949

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    I have limited experience with different programming languages. VBA, like the few other, older programming languages that I'm familiar with, only evaluate the "Do while" condition at the Loop/Do statement. This means that the entire loop is executed each time that the loop logic decides to execute the loop. (Wikipedia describes generic Do While loops here: https://en.wikipedia.org/wiki/Do_while_loop ). I think this is the reason that the Exit Do statement exists in those programming languages that have it -- is to allow the programmer to test the while condition (or other condition) and exit the loop at a point other than the Do/Loop statements.

    I would be interested to know if there are programming languages that behave the way you suggest this should. I can see how the behavior you describe would be detrimental to the Do While loops that I usually use.

    I know that this explanation really doesn't explain "why" programming languages work this way. We may need to dig quite deep into the "science" of developing high level languages to see what the rationale is for this. I can understand the logic in implementing the Do While .. Loop either way. I suspect that it is easier to implement an "Exit Do" type statement and let programmers choose if they want to test the while condition in the middle of the loop, than to have a statement that says "change loop condition variable but don't test it yet" kind of statement.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Please Login or Register  to view this content.
    Why doesn't the For loop exit as soon as 11 is assigned to i?
    Last edited by shg; 03-11-2016 at 05:16 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    @shg - Well actually that example doesn't worry me. It's merely a simple FOR...NEXT loop - I don't expect that loop to evaluate a fail condition during the loop.

    Whereas on a DO...WHILE or DO...UNTIL it implies (to me anyway!) that the condition is to be evaluated during the loop, not at the start/end of each iteration.

    It's no big deal if VBA cant do this as I can always return to nested Ifs (i.e. IF NOT...THEN ELSEIF NOT...THEN ELSEIF NOT etc)

    I just find it frustrating that some threads out there indicate that there IS a way that this can be done in VBA/VBS. It don't work for me.
    Last edited by mc84excel; 03-13-2016 at 06:41 PM. Reason: clarify FOR...NEXT

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Thanks Mr Shorty. Food for thought.

    Quote Originally Posted by MrShorty View Post
    VBA, like the few other, older programming languages that I'm familiar with, only evaluate the "Do while" condition at the Loop/Do statement. This means that the entire loop is executed each time that the loop logic decides to execute the loop.
    That's what I thought too. However I found threads out there that indicated that there is a way of writing the loop in VBA to make it exit immediately on fail. I couldn't get this to work which is why I started this thread.

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Why not just use the VBA function CBOOL to evaluate your arguments i.e. this will return False...

    Please Login or Register  to view this content.
    ...and this will return True:

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  11. #11
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    @shg:
    Why doesn't the For loop exit as soon as 11 is assigned to i?
    is/was that a rhetorical question?

    It was always my understanding that the loop control values were held internally, and that you cannot change them within the loop. Clearly, that is not the case as this example demonstrates ... why you shouldn't interfere with a loop counter:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Quote Originally Posted by InvisibleMan View Post
    @shg: is/was that a rhetorical question?]
    Yup.

    It was always my understanding that the loop control values were held internally, and that you cannot change them within the loop]
    You can change them, as your code demonstrates, but it's horrible practice.

  13. #13
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    You can change them, as your code demonstrates, but it's horrible practice.
    Yep. But I really did think they were sacrosanct. Oh well, we live and learn.

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    @ Trebor - Because the problem isn't about determining the condition result. The problem is that VBA only tests the condition change at the start/end of each loop.

    It would appear that WHILE or UNTIL loops in VBA (despite what other forum threads out there are saying ) cant test the condition fail while the current loops iteration is running.

    The only options so far are: 1) to repeatedly insert code into the loop to check the condition and then Exit Do or 2) set up an On Error Go To based on the condition fail

    I think I will go back to nested IF...ELSEIF - It's is a superior method to either of the above two workarounds

  15. #15
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    The OP has an open loop, i.e. neither the Do nor the Loop statements involve While or Until.

    One can construct loops where the "continue looping" condition is tested at the Do or Loop statement.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  16. #16
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,949

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    despite what other forum threads out there are saying )
    It might provide some context or some discussion fodder if you could provide one or more of these forum threads that are claiming this kind of Do While..Loop behavior. My gut feel at this point is that either the readers of these posts are misunderstanding what is being written, or the authors of these posts are misunderstanding the exact behavior of Do While...Loops.

    I think I will go back to nested IF...ELSEIF - It's is a superior method to either of the above two workarounds
    It might be interesting to explain exactly what this section of code is supposed to be doing. I have always envisioned significant differences between the "branching" control structure of a block If..then..elseif..endif and the "repeating" control structure of a loop. In a block if, I use one or more boolean variables/expressions to choose which sequence of statements to execute. A loop structure repeats a block of statements multiple times, where the boolean variable/expression is used to determine how many times the loop is executed. The two programming structures can be nested and otherwise combined in many different ways, but I don't usually think of trying to substitute a "loop" structure for a "branched" structure like this. It makes me wonder exactly what you are doing, and if trying to use a loop is even a better approach for what you are trying to do.

    If you are interested in furthering the discussion, it might be valuable to describe where you are getting your information about this behavior of Do While..Loops and what you are trying to do that would substitute a Do While..Loop for a Block If.

  17. #17
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Quote Originally Posted by mc84excel View Post
    Whereas on a DO...WHILE or DO...UNTIL it implies (to me anyway!) that the condition is to be evaluated during the loop, not at the start/end of each iteration.
    It's no big deal if VBA cant do this as I can always return to nested Ifs (i.e. IF NOT...THEN ELSEIF NOT...THEN ELSEIF NOT etc)
    If it did that the implication would be that you would be testing your conditions on every line (or even worse - testing it at every change of anything in excel!) which seems likely to be inefficient - as opposed to just testing it as those places where it is expected to change.

    Also, if one started nesting these you could find yourself running multiple complex tests on multiple variables at every line (despite there being no reference to them on those lines) which could get near impossible to follow.

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

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    More Unicorns?
    If posting code please use code tags, see here.

  19. #19
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    deleted post
    Last edited by mc84excel; 03-20-2016 at 06:50 PM.

  20. #20
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Quote Originally Posted by MrShorty View Post
    It might be interesting to explain exactly what this section of code is supposed to be doing. I have always envisioned significant differences between the "branching" control structure of a block If..then..elseif..endif and the "repeating" control structure of a loop. In a block if, I use one or more boolean variables/expressions to choose which sequence of statements to execute. A loop structure repeats a block of statements multiple times, where the boolean variable/expression is used to determine how many times the loop is executed. The two programming structures can be nested and otherwise combined in many different ways, but I don't usually think of trying to substitute a "loop" structure for a "branched" structure like this. It makes me wonder exactly what you are doing, and if trying to use a loop is even a better approach for what you are trying to do.
    Thanks Mr Shorty. I've given up expecting a VBA loop that will do this but I'll post a real world example at bottom of this post FYI.

    This function is an opening check on a complex project. It checks each of the start-up/install pre-reqs to ensure that all of these checks return TRUE. If any check returns FALSE, the function won't bother checking the remaining checks and return the result FALSE (i.e. fail)

    Now I know that this function isn't loop material and that nested Ifs are doing their job. But it occurred to me that if I could alter this code to a loop it would (slightly) improve code readability. (As soon as one of these check functions returned FALSE, the loop would automatically exit and the functions result would return FALSE)
    (BTW the final part of the loop would have contained an Exit Do - it would never have been a true loop)


    Code Readability was the only reason I was interested in seeing if Nested Ifs could be substituted with a Loop.


    Please Login or Register  to view this content.
    Last edited by mc84excel; 03-20-2016 at 07:01 PM.

  21. #21
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    It's a perfect case for the check procedures raising an error handled by the calling routine.

  22. #22
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    Quote Originally Posted by shg View Post
    It's a perfect case for the check procedures raising an error handled by the calling routine.
    [blinks] Yes. You are correct. Thanks shg. (I had been doing the error handling in each sub-function )

  23. #23
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Loops are driving me loopy! How to exit a loop as soon as condition fails

    You're welcome.

+ 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. Loops with saved variables for each loop to be compared after loop is finished?
    By flabb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2016, 07:48 AM
  2. [SOLVED] On Error fails on Multiple Loops
    By FinlayH in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2013, 09:45 AM
  3. [SOLVED] Exit for each loop condition[s]
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 07-16-2013, 10:44 AM
  4. [SOLVED] Nested Loops and Exit For Statement
    By RGrunden in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2012, 02:26 PM
  5. If condition fullfilled exit sub
    By fatalcore in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-25-2011, 11:35 AM
  6. loopings driving me loopy
    By khalid79m in forum Excel General
    Replies: 1
    Last Post: 01-09-2007, 05:30 PM
  7. [SOLVED] Going loopy on a loop
    By John in Surrey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-20-2005, 01:00 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