Share via

Stop an infinite loop macro using another macro

Anonymous
2013-11-26T14:45:01+00:00

Hi,

I have a button which starts an infinite loop macro.

Is there a way that I can stop this by running another macro?

Regards,

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-11-26T15:45:03+00:00

You might be able to do it this way - both macros have a button to execute them.  The MsgBox statements are just for example purposes.

'made Public so that the Start/Stop macros can be

'in separate code modules if needed/desired.

Public StopThisNonsense As Boolean

Sub StartInfiniteLoop()

  MsgBox "Starting Loop"

  StopThisNonsense = False

  Do Until StopThisNonsense

    DoEvents ' need this to recognize other events like button-click!

  Loop

End Sub

Sub MyOtherMacro()

'best to have a button to run this macro also!!

  StopThisNonsense = True ' stop that perpetual loop in Workbook_Open()

  'do more stuff here

  MsgBox "Loop Halted"

End Sub

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-11-27T05:42:59+00:00

    Thanks..

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-11-26T15:16:15+00:00

    It would make more sense to create a macro where the loop is not infinite i.e. it loops until the intended task is complete, by having a check condition before each iteration.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-11-26T15:14:50+00:00

    No - you need to have code within your looping code that has a check and a way out. Otherwise, use Ctrl-Break.

    But, it depends on what you mean by "infinite loop macro". If you have a macro that schedules itself using the application.ontime method, then you can use use schedule:=False parameter to cancel the next scheduled run of the macro.

    Was this answer helpful?

    0 comments No comments