First of all this only happens with this code if the code is being run from the windows task scheduler - I am not sure what that has to do with it, but it might be relevant. Here is a related post: https://www.excelforum.com/excel-pro...scheduler.html. I can run this code in real time and it runs without error to completion.
In the code below, there are two DoEvents with the comment "Added just because." These lines of code are highlighted in green. According to the log, when these lines are included the code stops on
AppendToLog "Saving Workbook - commented out"
If I remove these two lines, the code stops on
AppendToLog "Do Event 3 Start"
Which is exactly two non-comment instructions later.
In other words, the code appears to execute a fixed number of steps and then stops. No error message. Excel is still running, but using no CPU.
Just for grins, I set and unset break points. To see if "toggling" breakpoints would clear the issue. Nope!
I am wondering if anybody has an idea as to why this is happening and why only if launched from the task scheduler. I have other tasks that run successfully from the task scheduler.
Sub QueryGeneral(RunNumber As Long)
Dim cl As Range
Dim StartTime As Date, EndTime As Date
Dim ConnName As String, SQLName As String
AppendToLog "Query General " & RunNumber & " called"
For Each cl In Range("Table_Queries[Run]")
If cl.Value = RunNumber Then
Application.StatusBar = "Running " & cl.Offset(0, -3).Value
cl.Offset(0, 1).Value = Now()
StartTime = Now()
ConnName = cl.Offset(0, -2).Value
SQLName = cl.Offset(0, -1).Value
Application.Calculation = xlManual
AppendToLog "Calling SubSQL with " & ConnName
SubSQL ConnName, SQLName
AppendToLog "SubSQL Complete with " & ConnName
Application.Calculation = xlAutomatic
cl.Offset(0, 2).Value = Now()
EndTime = Now()
cl.Offset(0, 3).Value = EndTime - StartTime
AppendToLog "Do Event 1 start"
DoEvents
AppendToLog "Do Event 1 End"
AppendToLog "Do Event 2 Start"
'DoEvents
AppendToLog "Do Event 2 End"
AppendToLog "Saving Workbook - commented out"
DoEvents ' Added just because
DoEvents ' Added just because
'ThisWorkbook.Save ' ***** because it will fail on running autofial
AppendToLog "Workbook saved"
AppendToLog "Do Event 3 Start"
DoEvents
AppendToLog "Do Event 3 End"
AppendToLog "Executing End if"
End If
DoEvents
Next cl
AppendToLog "Query General " & RunNumber & " exiting."
Application.StatusBar = False
End Sub
Bookmarks