title: VBA - Difficulty interrupting Application.OnTime
body: <p>I am having difficult interrupting a looped <code>Application.OnTime</code> command. I have a macro I want to periodically loop every 5 seconds (<code>CommandButton221</code>), but then I want to be able to cut the loop off by pressing <code>StopButton</code>.</p>
  
  <pre><code>Private Sub CommandButton221_Click()
      If StopMacro = True Then Exit Sub
      '[Code that I want looping, not relevant to the question]
      testup1
  End Sub
  
  Public Sub testup1()
      RunWhen = Now + TimeValue("00:00:05")
      Application.OnTime RunWhen, "CommandButton221_Click", , True
  End Sub
  
  Public Sub StopButton_Click()
      StopMacro = True
  End Sub
  </code></pre>
  
  <p>Any suggestions as how to achieve  this would be hugely appreciated!</p>