Home Stuck in infinite loop. Any way to break vba into Code when Control+Break doesn't work
Reply: 2

Stuck in infinite loop. Any way to break vba into Code when Control+Break doesn't work

Kam
1#
Kam Published in 2018-01-12 20:13:29Z

I have code stuck. It might be in an infinite loop. Not sure.

Is there a way to break the program to stop at the current line of code that it is running on?

I'd like to avoid shutting down excel because I want to be able to catch where in the loop it is, and by going into the code, I will be able to tell how much processing was done. I would like to break into the code, if possible.

It is stuck on hour glass.

Ctrl+Break doesn't seem to work

Seems like the running code has hijacked all the quota that cpu is giving to excel.

If there is nothing I can do now, is there something in the future I can do to where I can more easily break into the code?

I'm thinking that an intermittent wait within a loop might be a feasible solution.

Vba
braX
2#
braX Reply to 2018-01-12 20:15:14Z

In the future, include a DoEvents inside the loop. It will run a little slower, but you will be able to use Ctrl+Break to stop it from running.

AJD
3#
AJD Reply to 2018-01-12 21:53:53Z

Create a progress dialog when entering the loop and include a Cancel button. Within your loop check for the Cancel signal/event. This also gives you some flexibility on how you react to the Cancel - you could stop the loop and display key information in a new dialog box (for example).

Basic steps to achieve what I have described (not necessarily the most elegant or the most re-useable, but simple enough for those of lesser experience):

  • create a modeless (not modal) Form with either suitable labels or a progressbar item (for visual effect). Include a public property (Boolean) for Cancel (e.g. boolCancel)
  • Place a button on form and onClick set boolCancel = True

  • In your main code, show the form just before your problem loop.
    while in your loop you can update some label or progress bar on the
    form so that you have a visual indication of whether the loop is
    doing something of value or if it is now simply spinning its wheels. How you do this depends on what your loop is doing.

  • Also while in your loop check your boolCancel value. If true then display any state information you want and break from the loop.

  • If your loop ends normally, hide/unload the progress dialog.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306708 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO