Avatar of Travis Hydzik
Travis Hydzik
Flag for Australia asked on

Office VBA stop code execution via form

I would like some one to show me how to set up code and a form so that when the code is running, a stop command on the form will halt the running code nicely.

i have attached an example, just to show what I mean.
when you click run, it runs some code in the background. then what needs to happen is when stop is clicked, the code is stopped nicely in a way to branch to some closing code.
Userform-break-example.xls
Visual Basic ClassicMicrosoft Excel

Avatar of undefined
Last Comment
Curt Lindstrom

8/22/2022 - Mon
Curt Lindstrom

Use "Unload Me"

See attached modified file

Curt
Userform-break-example-1.xls
Travis Hydzik

ASKER
thanks for the reply,

did you get this to work, as I can't?

the form doesn't break the code, just hangs when I try to press stop.
Curt Lindstrom

Strange, it exits the form immediately when I press the Stop button.

For more information in how to design a userform try the links to this website.

http://www.contextures.com/xlUserForm01.html

http://www.contextures.com/xlUserForm02.html

Cheers,
Curt
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Travis Hydzik

ASKER
okay, I believe you havent pressed the run button first.

the whole idea is to stop some running code.
Curt Lindstrom

Yes you're right! I didn't read your question properly :-)
Curt Lindstrom

I don't think it's possible to break the running code with a Stop button on the Userform. Your code calling the auto_open cannot even be stopped with Ctrl/break.

Try this one with two For loops. As you can see the Run button remains pressed during the execution of the code which means that no other buttons can be pressed on the form. You can however stop this code from running with Ctrl/break.

Curt
Userform-break-example-2.xls
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ledigimate

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Travis Hydzik

ASKER
Genius, thanks, very handy to know.
Curt Lindstrom

Simple indeed.  
Sometimes I just have to show my ignorance to keep learning something new :-)
Thanks for the lesson Ledigimate, Sorry for misleading you thydzik.

Curt