title: VBA: How do I really stop Application.onTime()?
body: <p>I have already read some questions/threads (e.g. <a href="http://www.mrexcel.com/forum/excel-questions/46586-application-time-wont-turn-off.html" rel="nofollow noreferrer">this</a> and <a href="http://www.cpearson.com/excel/OnTime.aspx" rel="nofollow noreferrer">this</a>) about how to stop the VBA <code>Application.OnTime</code> procedure, but I just can't get it to stop!</p>
  <p>I am using it to pull some data every <em>x</em> seconds. I understand that when I call the <code>OnTime()</code> method I need to pass to it the same time value that was used to schedule the event.</p>
  <p>I have also tried to introduce multiple commands (that try to cause an error for example) to stop the execution but it still doesn't work! The program just keeps running... This is how my code looks:</p>
  <p>In Worksheet code I have:</p>
  <pre><code>Public TimerActive As Boolean
  Public tick As String
  Public idx As Long
  Public counter As Long
  
  Public Sub UpdateOff_Click()
  
      TimerActive = False
      tick = &quot;Off&quot;
      counter = 1
      idx = 1
      
      Call StopTimer(idx, counter, tick, TimerActive)
      End ' force quit??
  
  End Sub
  
  Public Sub UpdateOn_Click()
  
      TimerActive = True
      tick = &quot;live&quot;
      counter = 1
      idx = 1
      
      Call StartTimer(idx, counter, tick, TimerActive)
      
  End Sub
  </code></pre>
  <p>and in a separate module I have:</p>
  <pre><code>Public fireTime As Date
  
  Sub StartTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
  
      fireTime = Now + TimeValue(&quot;00:00:05&quot;)
      sMacro = &quot;  'pullData &quot; &amp; idx &amp; &quot; , &quot; &amp; counter &amp; &quot;, &quot; &amp; Chr(34) &amp; tick &amp; Chr(34) &amp; &quot;, &quot; &amp; TimerActive &amp; &quot;'&quot;
      Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=True
  
  End Sub
  
  Sub StopTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
     
      sMacro = &quot;cause error&quot; ' cause an error (by giving false sub name so program stops?
  
      Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=False
      End
  
  End Sub
  
  
  Public Sub pullData(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
  DoEvents
  If TimerActive = True Then
      
      ' pull the data do some stuff, print the data, etc...
  
      idx = idx + 1
      counter = counter + 1
      tick = tick + &quot; .&quot;
      If counter = 6 Then
          counter = 1
          tick = &quot;live&quot;
      End If
  
      Call startTimer(idx, counter, tick, TimerActive)
  
  End If
  
  End Sub
  </code></pre>
  <p>I understand that I may have introduced a few extra measures to stop the execution but none seem to work!</p>