Stopping a VBA loop that contains a dialog box!

Example spreadsheets and comments on example spreadsheets.
Post Reply
User avatar
Derek27
Posts: 23682
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

As the title suggests, does anyone have a solution to this problem? It's completely crazy having to kill the Excel task from the command line or task manager just to stop code from running!

Capture.PNG
You do not have the required permissions to view the files attached to this post.
foxwood
Posts: 394
Joined: Mon Jul 23, 2012 2:54 pm

If I force that error and click on "OK" it opens the source in debug mode at the bad line as if there was a breakpoint there.

My VB | Tools | Options has "break on unhandled errors" (Excel 365) ...
Image1.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
Derek27
Posts: 23682
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

Cheers foxwood, I'll give that a try.
User avatar
conduirez
Posts: 298
Joined: Tue May 23, 2023 8:25 pm

I assume you are testing code, this is just an idea, it may help you, but without seeing the code that forces this Message Box to be called, it is difficult to see what is happening, but you could try adding

Application.DisplayAlerts = False

at the start of the loop in your code, I think it should but I am not 100 percent certain if it will work in your circumstances.

do not forget to add

Application.DisplayAlerts = True

after the end of your loop.

You may have to use Application.EnableEvents = False and Application.EnableEvents = True as well.

Hopefully this should stop multiple system message boxes appearing, like you have had whilst testing code, however you will need to collect error information yourself and resume next on errors.
User avatar
Derek27
Posts: 23682
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

conduirez wrote:
Fri Nov 24, 2023 12:22 am
I assume you are testing code, this is just an idea, it may help you, but without seeing the code that forces this Message Box to be called, it is difficult to see what is happening, but you could try adding

Application.DisplayAlerts = False

at the start of the loop in your code, I think it should but I am not 100 percent certain if it will work in your circumstances.

do not forget to add

Application.DisplayAlerts = True

after the end of your loop.

You may have to use Application.EnableEvents = False and Application.EnableEvents = True as well.

Hopefully this should stop multiple system message boxes appearing, like you have had whilst testing code, however you will need to collect error information yourself and resume next on errors.
Actually, it wasn't a loop, although the problem can occur when a dialog box appears in a loop. I changed some code while it was running that produced a compile error. I've seen in places you can stop code by pressing CTRL + BREAK, but neither of my PCs have a break key and I'm not sure that would work when you've got a dialog box on the screen.
User avatar
ODPaul82
Posts: 689
Joined: Sun May 08, 2011 6:32 am
Location: Digswell Herts

If the sub or func that it's throwing that error at does exist there's a chance that the code module could have become corrupted during compilation. Can happen from time to time.
When it does I create a new code module inside the file, copy all of the code from the offending module, delete the original then rename the new module to what it had been

I always have Option Explicit set as well (Tools -> Options -> Editor -> Require Variable Declaration)

Try running a debug -> Compile VBA Project which should also take you to where there is any offending code as well.
User avatar
Derek27
Posts: 23682
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

ODPaul82 wrote:
Fri Nov 24, 2023 5:53 am
If the sub or func that it's throwing that error at does exist there's a chance that the code module could have become corrupted during compilation. Can happen from time to time.
When it does I create a new code module inside the file, copy all of the code from the offending module, delete the original then rename the new module to what it had been

I always have Option Explicit set as well (Tools -> Options -> Editor -> Require Variable Declaration)

Try running a debug -> Compile VBA Project which should also take you to where there is any offending code as well.
You can't do that with a dialog box on the screen. All you can do is click on OK, and as it's in a loop it immediately reappears.
User avatar
ODPaul82
Posts: 689
Joined: Sun May 08, 2011 6:32 am
Location: Digswell Herts

I meant when you first open it, sorry should have been clearer
User avatar
conduirez
Posts: 298
Joined: Tue May 23, 2023 8:25 pm

There is one chance that pressing and holding down the 'esc' key may work in your case. If this does not work try to hit the 'esc' key twice quickly. Apart from that I am out of ideas.

For reference, I know on Dell models the substitute for 'ctrl' - 'break'' is pressing 'ctrl' - 'fn' - 'B' keys together, but with a dialogue box that keeps repeating this will not work.

Another thing to check before your code runs is that all your references are still ticked.
foxwood
Posts: 394
Joined: Mon Jul 23, 2012 2:54 pm

Run the On-Screen Keyboard (Windows key + R and type the program name osk) that has a Ctrl and Pause/Scroll keys.

Not sure if it will work though since CtrlBrk has a special scancode !

On the physical keyboard Ctrl + NumLock / Ctrl + ScrLock used to work for Pause / Break respectively on small layout keyboards back in the day - may work.
User avatar
Derek27
Posts: 23682
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

Thanks for the ideas guys. I think the easy solution would be to stop the code before editing but I'll try out those suggestions. :)
Post Reply

Return to “Bet Angel - Example spreadsheets”