title: VBA ontime cancel scheduling
body: <p>I have written a macro that runs at 15:30pm every workday when a workbook is first opened.
  When the workbook is closed it tries to open itself the next time the macro is scheduled to run. I have tried to turn the scheduler to false and am getting an error. Code below.
  Has anyone any ideas why this isn't working?</p>
  <pre><code>Private Sub Workbook_Open()
      Application.OnTime TimeValue(&quot;15:30:00&quot;), &quot;MacroTimeTest&quot;
  End Sub
  
  public dtime as date
  
  Sub MacroTimeTest()
  
      dtime = (Format(Application.Evaluate(&quot;workday(today(), 1)&quot;), &quot;DD/MM/YY&quot;) &amp; &quot; &quot; &amp; TimeValue(&quot;15:30:00&quot;))
  
      'other code has been deleted doesn't affect dtime variable 
  End Sub
  
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
      'I have tried replacing false with 0 etc but it didn't make a difference
      Application.OnTime earliesttime:=dtime, procedure:=&quot;MacroTimeTest&quot;, schedule:=False
  
  End Sub
  </code></pre>