title: Scheduled VBA task and 'Application.OnTime'
body: <p>I have the following <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications" rel="nofollow">VBA</a> code which is working well. It's calling another VBA <code>Sub</code> without any trouble:</p>
  
  <pre><code>Public Sub AutoPrintMissingHistoric()
      Dim qdf As DAO.QueryDef
      Dim rcs As DAO.Recordset
      Dim db As DAO.Database
      Dim j As Integer
      Dim flag As Boolean
      Dim i As Long
      Dim value_start, value_end As String
      Dim tmp As Date
      Dim wbRiskedge As Workbook
      Dim wsAccueil As Worksheet
      Dim wsHistoric As Worksheet
  
      Set wbRiskedge = Workbooks(StrWbRiskedge)
      Set wsAccueil = wbRiskedge.Worksheets(StrWsAccueil)
      Set wsHistoric = wbRiskedge.Worksheets(StrWsHistoricMissing)
      If FistTime = True Then
          Call Initialisation.CleanTab
      Else
          FistTime = True
          Call Initialisation.Initialisation
      End If
      vDelay = 5
      Cpt = Cpt + 1
      Set db = DBEngine.OpenDatabase(strDB)
      Set qdf = db.QueryDefs("Get_missing_fixings")
      If Cpt &lt;= wsAccueil.Range(ManualListLetter &amp; "1").End(xlDown).Row Then
          Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text
          qdf.Parameters("arg1") = wsAccueil.Cells(Cpt, ManualListLetter).Value
          Set rcs = qdf.OpenRecordset
          j = 0
          i = 1
          flag = False
          If Not rcs.EOF Then
              rcs.MoveLast
              rcs.MoveFirst
              While Not rcs.EOF
                  j = 0
                  While j &lt; rcs.Fields.Count
                      If flag = False Then
                          With Cells(i, j + 1)
                              If .Value = "" Then
                                  .Value = rcs(j).Name
                                  .Font.Bold = True
                                  .HorizontalAlignment = xlCenter
                                  .VerticalAlignment = xlBottom
                              End If
                          End With
                      Else
                          Cells(i, j + 1).Value = rcs(j).Value
                      End If
                      j = j + 1
                  Wend
                  If flag = False Then
                      flag = True
                  End If
                  i = i + 1
                  rcs.MoveNext
              Wend
              Call ChangeMinMax(rcs.RecordCount, CellMinDate, CellMaxDate, wsHistoric)
              Call ParseParameters
              Call SetReutersFunction
          End If
          rcs.Close
          qdf.Close
          db.Close
          wsHistoric.Calculate
          Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text &amp; " - Next Function: FindMissingValue.AutoFindMissingValue"
          sToCall = "FindMissingValue.AutoFindMissingValue"
          MTimeGT = Time + TimeValue("00:00:" &amp; vDelay)
          Application.OnTime MTimeGT, sToCall
      End If
  End Sub
  </code></pre>
  
  <p>I put the execution of this process in a scheduled task. But apparently my code is not well executed: the <code>FindMissingValue.AutoFindMissingValue</code> Sub is not called because Excel just closes.</p>
  
  <p>I think it's because of <code>Application.OnTime MTimeGT, sToCall</code>... What could be the reason?</p>
  
  <p>Here you've the code of <code>FindMissingValue.AutoFindMissingValue</code></p>
  
  <pre><code>Sub AutoFindMissingValue()
      Dim wbRiskedge As Workbook
      Dim wsAccueil As Worksheet
      Dim wsHistoric As Worksheet
      Dim i, nbResult As Long
  
      Set wbRiskedge = Workbooks(StrWbRiskedge)
      Set wsAccueil = wbRiskedge.Worksheets(StrWsAccueil)
      Set wsHistoric = wbRiskedge.Worksheets(StrWsHistoricMissing)
      If Left(wsHistoric.Range(ReutersFormula).Text, 13) Like "Retrieving...*" = True Then
          sToCall = "FindMissingValue.AutoFindMissingValue"
          MTimeGT = Time + TimeValue("00:00:05")
          Application.OnTime MTimeGT, sToCall
          Exit Sub
      End If
      i = WorksheetFunction.CountA(Columns(DateColumn &amp; ":" &amp; DateColumn))
      If WorksheetFunction.CountA(Columns(ColumnResearchVResult &amp; ":" &amp; ColumnResearchVResult)) &gt; 0 Then
          wsHistoric.Range(FirstCellResearchVResult &amp; ":" &amp; ColumnResearchVResult &amp; WorksheetFunction.CountA(Columns(ColumnResearchVResult &amp; ":" &amp; ColumnResearchVResult))).ClearContents
      End If
      nbResult = wsHistoric.Range(FirstResult).End(xlDown).Row
      wsHistoric.Range(ColumnResearchVResult &amp; LineResearchVResult - 1).Value = "Results"
      If WorksheetFunction.CountA(Columns(DateColumn &amp; ":" &amp; DateColumn)) &gt; 1 Then
          wsHistoric.Range(FirstCellResearchVResult &amp; ":" &amp; ColumnResearchVResult &amp; i).FormulaLocal = "=RECHERCHEV($" &amp; DateColumn &amp; "$" &amp; LineResearchVResult &amp; ":$" &amp; DateColumn &amp; "$" &amp; i &amp; ";" &amp; FirstLockResult &amp; ":$" &amp; ValueResultColumn &amp; "$" &amp; nbResult &amp; ";2;0)"
      End If
      Application.StatusBar = wsAccueil.Cells(Cpt, ManualListLetter).Text &amp; " - Next Function: FindMissingValue.AutoPutResultInDb"
      sToCall = "FindMissingValue.AutoPutResultInDb"
      MTimeGT = Time + TimeValue("00:00:01")
      Application.OnTime MTimeGT, sToCall
  End Sub
  </code></pre>