+ Reply to Thread
Results 1 to 4 of 4

Exit a loop from within a user form.

  1. #1
    Forum Contributor
    Join Date
    02-26-2009
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    286

    Exit a loop from within a user form.

    The sub “CheckDate” in module 10 has a For Each loop. Within the loop a user form “frmFollowUp” is called. If I use “Unload Me” or “frmFollowUp.Hide” in the “Cancel” command button the current form closes but the loop carries on bringing up the user form again as part of the continuing loop.
    What code in the “Cancel” command button can I use to close the user form and exit the loop as well?
    The attached spreadsheet has the user form and part of the loop code.
    Attached Files Attached Files

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Exit a loop from within a user form.

    well of course the loop carries on! you are not terminating it. so are you wanting to terminate the code when the form is closed/unloaded the FIRST time? when you show a form like the way you are doing, code execution halts because the form is what is called a dialog. and a dialog takes precedence over literally anything else. that's why the form doesn't reload and the loop continues until you close it. thus, if what I speculate you want is true, you can do it a countless number of ways. one way, which comes to my mind, is to check a global var. a boolean perhaps. so, for instance, you would write this in the primary module:
    Please Login or Register  to view this content.
    and then, you would write this behind your cancel button:
    Please Login or Register  to view this content.
    and then, you would write this at the end of your primary routine before the loop begins its next go-round:
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    02-26-2009
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    286

    Re: Exit a loop from within a user form.

    Thanks that worked exactly as I wanrted it to. Cheers Sandy

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Exit a loop from within a user form.

    good luck sandy.

+ 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. [SOLVED] Loop with Command Buttons on User Form
    By SFtdm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-19-2018, 09:56 AM
  2. Loop to refresh user form TextBoxes
    By lanceloz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-28-2017, 02:36 AM
  3. User Form Control Exit with Frames
    By Rhudi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2017, 01:44 PM
  4. Exit Previous User Form
    By AmirSoft in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-28-2014, 06:50 AM
  5. Validation not allowing user to exit form
    By Damian37 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-03-2014, 11:02 AM
  6. Save and exit workbook when user form closes
    By savio21 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2011, 11:21 AM
  7. Exit sub from user form problem...
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-10-2008, 10:54 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