title: How do I create a stop for the Application.OnTime event in my code?
body: <p>I have some code here that works perfectly except for when I try to close the Excel Sheet. I have tried to program the timer to stop when i close the Workbook but it does not seem to be working. Whenever I close the workbook it automatically re-opens. Any help with tweaking my code would be gratly appreciated. Here it is:</p>
  
  <p>Code in Module 1:</p>
  
  <pre><code>    Dim RT1 As Date
      Dim Lunch As Date
      Dim ApT As Date
      Dim RT3 As Date
      Dim NextTick As Date
  
      Public Sub UpdateTime()
      ' Places a bug fix for stopping the clock when closing the workbook
      Debug.Print "UpdateTime" &amp; Format(RunWhen, "00:00:00")
      ' Updates cell D8 with the Current time
      ThisWorkbook.Sheets(1).Range("D8") = Now()
      ' Set up next event 1 second from now
      NextTick = Now + TimeValue("00:00:01")
      Application.OnTime NextTick, "UpdateTime"
      End Sub
  
      Public Sub StopClock()
      ' Cancels the OnTime event
      On Error Resume Next
      Application.OnTime NextTick, "UpdateTime", , False
      End Sub
  </code></pre>
  
  <p>Code in Module 2:</p>
  
  <pre><code>    Sub PhoneHours()
      'Time left at the beginning of the day
      If Range("B12") &lt; Range("A3") Then Range("E12").FormulaR1C1 = "=(R[3]C[-3]-R[0]C[-3])-        (2*R[0]C[1])"
      'Time left after the first Research Time Has passed
      If Range("B12") &gt;= Range("A3") Then Range("E12").FormulaR1C1 = "=(R[3]C[-3]-R[0]C[-3])-        (1.75*R[0]C[1])"
      'Time left after Lunch and Second Research Time
      If Range("B12") &gt;= Range("B3") Then Range("E12").FormulaR1C1 = "=(R[3]C[-3]-R[0]C[-3])-        (0.5*R[0]C[1])"
      'Time left afetr Apple Time
      If Range("B12") &gt;= Range("D3") Then Range("E12").FormulaR1C1 = "=(R[3]C[-3]-R[0]C[-3])-        (0.25*R[0]C[1])"
      'Time left after Final Research Time
      If Range("B12") &gt;= Range("E3") Then Range("E12").FormulaR1C1 = "=(R[3]C[-3]-R[0]C[-3])"
  
      NextCheck = Now + TimeValue("00:00:10")
      Application.OnTime NextCheck, "PhoneHours"
  
      End Sub
  
      Sub StopCheck()
      ' Cancels the OnTime event
      On Error Resume Next
      Application.OnTime NextCheck, "PhoneHours", , False
      End Sub
  </code></pre>
  
  <p>Code in ThisWorkbook:</p>
  
  <pre><code>    Sub Worksheet_Deactivate()
      Call StopClock
      Call StopCheck
      End Sub
  
      Sub Workbook_Activate()
      Call UpdateTime
      Call PhoneHours
      End Sub
  
      Sub Workbook_Open()
      Call UpdateTime
      Call PhoneHours
      End Sub
  
      Sub Workbook_Close()
      Call StopClock
      Call StopCheck
      End Sub
  </code></pre>
  
  <p>Thanks in Advance!</p>