Implementing Cancels in VBA Loops

I like to allow users the opportunity to cancel out of long loops if they need to. I use a global variable, something like gbCanacel, which gets set to True when the user clicks the Cancel button.

CancelButton.PNG

Then my looping checks for this with an if statement, normally the last thing inside the next/loop statement. If it detects that gbCancel is True, then I reset gbCancel and exit the sub. With liberal use of DoEvents this works quite well.

If you implement Cancel, do you do it the same way. If not how and why!!!!

Comments

  1. JP says:

    What kind of form is that where the end user can click the cancel button while you’re in the middle of a VBA routine? My assumption was that while code is running, buttons are disabled. More detail please.

    –JP

  2. simon says:

    Ross
    Yep same approach, opposite logic (if bContinue? at the top)

  3. ross says:

    well one of us must be backwards then!

  4. MK says:

    This seems really cool. Is it possible to have a look at the code/workbook?

    Thanks.

Submit a Comment