I have a macro that downloads some information from the internet and I need to schedule it to run automatically starting at a certain time of day and running repeatedly both at set time intervals although occasionally at specific times output by the macro.
I have approached the problem in two ways, both of which have presented problems that I can't figure out. The first approach is using a timer sub that runs the macro using the Application.OnTime command with the runtime derived from a cell in the workbook that is updated by the macro.
This method works pretty well if I am on the computer doing something else. However, I have noticed that if I leave, even if the computer doesn't go to sleep, the program inevitably stops re-running after 30min or so. Sometimes it also stops re-running even when I'm active on the computer. Why is this? What could be causing this? Also, naturally, I can't schedule the program to open and run automatically from within the macro, so I turned to the VBScript & Task Scheduler method for this part of the solution.
I have, however, encountered two problems with the execution of the VBScript. First, is that it often fails to start when run by Task Scheduler. Second, is that when it does run, the Application.OnTime command within the macro doesn't work. This is a problem because the macro needs to run sometimes at irregular time intervals which are determined during the course of the day as the macro runs through its iterations. Why would the vbscript fail to run when task manager tries, and why would it not initiate the application.ontime command the way the macro does when I run it manually?
VBA Timer Code:
Sub Timer()
Dim Runtime As Date
If Time <= TimeValue("17:45:00") Then
Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 10
ElseIf Time > TimeValue("17:45:00") And Time <= TimeValue("18:15:00") Then
Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 13
ElseIf Time > TimeValue("18:15:00") And Time <= TimeValue("18:45:00") Then
Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 16
ElseIf Time > TimeValue("18:45:00") And Time <= TimeValue("19:15:00") Then
Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 19
End If
Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Calculate
If Time > Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Value Then
Runtime = Time + TimeValue("00:00:30")
Else
Runtime = Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Value
End If
Application.OnTime TimeValue(Runtime), "Swingtimer"
Workbooks("10am_Swing_Report").Sheets("Parameters").Range("C16").Value = Runtime
Workbooks("10am_Swing_Report").Save
End Sub
Thanks!
I've updated the Vbs code to include a validation that the runtime has been passed, but I'm still encountering a problem with Task Scheduler failing to start the task. I'm getting the following error: "Launch request ignored, instance already running" followed by: "Task Start Failed". I was originally thinking this was because the Vbscript was still running from the previous instance, but I'm getting this from the very beginning.
Option Explicit
Dim xlApp
Dim xlBook
Dim runtime
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
'xlApp.Application.DisplayAlerts = True
Set xlBook = xlApp.Workbooks.Open("C:\Users\DORIAN\Dropbox\Swing_Report.xlsm",1,False)
Set xlSheet = xlBook.Sheets("Parameters")
'xlBook.application.Visible = True
'MsgBox FormatDateTime(xlSheet.Range("B16").Value,3)
runtime = TimeValue(FormatDateTime(xlSheet.Range("B16").Value,3))
'MsgBox runtime
If Time > runtime Then
xlApp.Run "Swingtimer"
'MsgBox "Running..."
WScript.Sleep 30000
Else
xlApp.Application.OnTime runtime, "Swingtimer"
'MsgBox "Timer Set"
End If
xlbook.close True
WScript.Quit