+ Reply to Thread
Results 1 to 5 of 5

Having difficulty with exiting a Do loop

  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

    Having difficulty with exiting a Do loop

    I'm sure this is a rookie question but I rarely use the Do...While and Do...Until loops.

    I have been abusing GoTo in the past and am trying to mend my ways.

    I have a form that is shown from a calling sub. I want a loop in the calling sub to check for a boolean condition (set by a form property) and to exit the loop the instant that the condition is met. Instead the loop repeats at least once before picking up the condition change. Is there any way around this or do I need to go back to using GoTo?


    (Reading the above makes me realise I'm not making myself very clear, I will post some dummy code below to express my idea more clearly)

    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
    Registered User
    Join Date
    11-06-2013
    Location
    Phoenix, az
    MS-Off Ver
    Excel 2013
    Posts
    26

    Re: Having difficulty with exiting a Do loop

    The routine won't exit until it's completed the loop and checks the status of blnCloseForm.
    To have it exit where you have identified you would need to check the status of blnCloseForm after it returns from the sub routine and use and Exit Do.

    Something like this:

    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,270

    Re: Having difficulty with exiting a Do loop

    It's not going to exit until it hits the loop at the bottom and goes back up to the head of the loop where it checks the condition.

    If you don't want it to do the stuff between where the flag is set false and the bottom of the loop, you need to put it inside an If ... End If.

    In structure programming, you would probably call one or more subroutines that executed the code so it would only be a few lines inside the If block.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    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: Having difficulty with exiting a Do loop

    Quote Originally Posted by TMShucks View Post
    If you don't want it to do the stuff between where the flag is set false and the bottom of the loop, you need to put it inside an If ... End If.
    Quote Originally Posted by TMShucks View Post
    In structure programming, you would probably call one or more subroutines that executed the code so it would only be a few lines inside the If block.
    That's what I was doing in the meantime but it just felt wrong.

  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: Having difficulty with exiting a Do loop

    Quote Originally Posted by Bukovnik View Post
    The routine won't exit until it's completed the loop and checks the status of blnCloseForm.
    To have it exit where you have identified you would need to check the status of blnCloseForm after it returns from the sub routine and use and Exit Do.
    Of course! (slaps head). How could I overlook
    Please Login or Register  to view this content.
    ? Anyway, thanks for solving. +1

+ 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. Problem with do loop - does not appear to be exiting
    By jbd99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 05:17 AM
  2. Not exiting For loop
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2008, 03:50 PM
  3. New question about exiting a loop!
    By Canuckbulldog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2008, 08:36 AM
  4. [SOLVED] Loop Difficulty
    By smandula in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2006, 10:45 PM
  5. Exiting a Do Loop on error
    By Cloudfall in forum Excel General
    Replies: 7
    Last Post: 11-02-2005, 06:17 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