I have a button on my spreadsheet that when clicked, triggers a series of nested subs. Example:
Sub Button1_Click()
Macro1
Macro2
Macro3
Macro4
Macro5
Macro6
End Sub
Because the time to run all these subroutines can be rather lengthy, and the user cannot do anything else in excel while it is running, I was wondering if there was a way to create a button that, when clicked, can halt the running of all these subs? I've seen recommendations of adding DoEvent
to the code, however, I'm not sure where in my code to implement it.
Any insight would be much appreciated.
The DoEvents
statement will allow any lagging user input and handling of events.
What you probably want to do is overload the OnKey
event handler and use it to set the value of a global variable. Next in your code above you need to verify if that variable is set. If it is abort execution. Be sure to add DoEvents
statements in any moments in your code where you want this to run.
Optionally , w/o the OnKey
handler, DoEvents
will allow the user to hit ESC to stop the execution of your macro. But it will revert to debug mode and highlight the current executed row in your procedure. Not sure this is what you want however.
Having said that... what I personally think you should do FIRST is optimize your VBA code. I would assume there is probably a lot of room for improving the execution time of your procedure ( my tutorial ).
You can create Macro to perform below steps/operation and call when you want to abort the other running macros:
Step 1-- Menu bar -> Run -> Reset
Step 2-- Menu bar -> Run -> Break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.