title: VBA Code to end macro at a time
body: <p>I have been looking around for a while trying to find a way to just force the macro to stop running at 1:15 pm. I have found things that will close the workbook at a certain time but nothing that will keep the workbook open and just stop the macro. I believe it has to do with the <code>Application.OnTime "13:15:00"</code> feature but I am not sure how to get it to stop the macro at the 1:15 pm. </p>
  
  <p>This is what keeps my excel workbook running:
  First when I open the workbook, this kicks in:</p>
  
  <pre><code>Private Sub Workbook_Open()
      If Time &gt; TimeSerial(6, 45, 0) Then
      Call TimeStamp
      Else
      Application.Wait "06:45:00"
      Call TimeStamp
  End Sub
  </code></pre>
  
  <p>That will then start the TimeStamp Macro which is ran every 15 minutes using these global variables and coding:</p>
  
  <pre><code>Option Explicit
  Public RunWhen As Double
  Public Const cRunIntervalSeconds = 900 ' 15 minutes
  Public Const cRunWhat = "TimeStamp"  ' the name of the procedure to run
  
  Sub StartTimer()
      RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
      Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
          Schedule:=True
  </code></pre>
  
  <p>After this section is my TimeStamp Macro which ends with: <code>Call StartTimer</code> to schedule the next run time 15 minutes from the current time.</p>