+ Reply to Thread
Results 1 to 11 of 11

Macro to break loop/Sub on button press

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2007
    Posts
    36

    Macro to break loop/Sub on button press

    Hi all

    I am looking to exit a Sub (actual code is way too long to post here so just an example below) via a macro when the user presses a button.

    The sort of Sub (without all the extra code) is similar to the below code - just runs from top to bottom and then jumps back up to the start again - an endless loop only stoppable by the user.

    I had thought of just having a macro that changes a boolean variable (in this case Pressed) to true when run (via the button) but I cannot get the macro to execute (via the button) whilst the loop below is working.

    I really need to user to be able to stop this Sub so any help is very much appreciated.

    Redders

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: Macro to break loop/Sub on button press

    this part of the macro appears to be ok

    before the statement
    If Pressed = True then Goto Leave
    introduce message box
    msgbox pressed
    check whether the message given is true or false or 1 or 0.

    you can also use the statement
    If Pressed = True then exit sub

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

    Re: Macro to break loop/Sub on button press

    VBA is not multithreaded. It will not detect events (like a button press) and execute an event handler for it while other code is running.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro to break loop/Sub on button press

    I was wrong above. It is true that VBA is not multithreaded, but I just discovered a way to allow the thread to be interrupted and then return to it. I just happened to stumble on a post by user shg that mentions the built-in function DoEvents, which allows the user to press a button, have your code deal with it, then return to whatever was going on. Attached is a simple example. If you press the Test button, a form appears while a counter increments in A1. If you press Display Message, the process is interrupted while a msgbox pops up. If you press Halt, the whole thing halts.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-04-2009
    Location
    Golborne England
    MS-Off Ver
    2013
    Posts
    85

    Re: Macro to break loop/Sub on button press

    Quote Originally Posted by 6StringJazzer View Post
    I was wrong above. It is true that VBA is not multithreaded, but I just discovered a way to allow the thread to be interrupted and then return to it. I just happened to stumble on a post by user shg that mentions the built-in function DoEvents, which allows the user to press a button, have your code deal with it, then return to whatever was going on. Attached is a simple example. If you press the Test button, a form appears while a counter increments in A1. If you press Display Message, the process is interrupted while a msgbox pops up. If you press Halt, the whole thing halts.
    Hi 6StringJazzer,

    Can you provide the sample code in a Zip file. Work system restrictions prevent me from downloading XLSM files. This is something that I could really use. I have a VBA program that creates price lists and it could run for days and ineed a way of interupting it.

    Thanks,

    JimBobBowie

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to break loop/Sub on button press

    In that case you should use Ctrl-Break.

    Improving your code (with the help of this forum) can even make that combination redundant.



  7. #7
    Registered User
    Join Date
    08-04-2009
    Location
    Golborne England
    MS-Off Ver
    2013
    Posts
    85

    Re: Macro to break loop/Sub on button press

    Thanks snb,

    I am looking for a solution that allows the interuption to happen after the loop completes a cycle but before it starts a new cycle. I had a solution that worked but was given a new PC and now with W-7 and office 2010 it does not work.

    JimBobBowie

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to break loop/Sub on button press

    In that case you'd better start a separate thread to solve that issue instead of 'hijacking' someone else's.

  9. #9
    Registered User
    Join Date
    08-04-2009
    Location
    Golborne England
    MS-Off Ver
    2013
    Posts
    85

    Re: Macro to break loop/Sub on button press

    Sorry, Didn't intend to hijack the thread I only thought that the sample file was a possible solution to my problem and I can't see it. I will wait until I get home tonight and I can view it there.

    Thanks,

    JimBobBowie

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro to break loop/Sub on button press

    It sounds like the same problem with the same solution so here's the same file as a zip.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    08-04-2009
    Location
    Golborne England
    MS-Off Ver
    2013
    Posts
    85

    Re: Macro to break loop/Sub on button press

    Thanks very much, I can make that solution work for what I am doing.

    Cheers,

    JimBobBowie

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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