Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow users to quit out of long-running VBA tasks?

I have a routine that examines thousands of records looking for discrepancies. This can take upwards of 5 minutes to complete and although I provide a progress bar and elapsed time count, I'm not sure I want to encourage folk pressing ctrl-break to quit the report should it be taking longer than expected.

A button in the progress bar won't work as the form is non-modal, so is there any neat way of allowing users to quit in this situation?

like image 242
Lunatik Avatar asked Jul 03 '09 10:07

Lunatik


People also ask

How do you break VBA code when running?

In VBA, you can stop your macro execution manually with the Esc key or by pressing Ctrl+Break.

Can you stop a macro while it's running?

More videos on YouTube If the Macro is simply in a continuous loop or is running for too long you can use one of these keyboard shortcuts to kill it: Esc hit the Escape key. Ctrl + Break hit Ctrl key and then the break key, which is also the pause key.

Is there a wait function in VBA?

Wait method is available within Excel as a VBA function, as opposed to Sleep (see below). You can use it to specify that a macro is paused for a specific period of time.

How do you sleep in Excel VBA?

Follow the below steps to use Sleep Function in Excel VBA: Step 1: Go to the Developer tab and click on Visual Basic to open VB Editor. Step 2: Once the VB Editor is open click on Insert Tab and then click on modules to insert a new module. Step 3: Now use the declaration statement to use sleep function.


1 Answers

You need DoEvents and a variable whose scope is greater than the scope of what you're running. That is, if it's just a procedure, you need a module level variable. If it's more than one module, you need a global variable. See here

Stopwatch at DDoE

Normally, the VB engine will tie up the processor until it's done. With DoEvents, however, VB allows the processor to work on whatever is next in the queue, then return to VB.

like image 100
Dick Kusleika Avatar answered Sep 24 '22 07:09

Dick Kusleika