Sub RunOnTime()
Application.OnTime Now+TimeValue("00:01:00"), "openBK"
End Sub
Once the OnTime code is run, the workbook containing the OnTime code can be closed, but the Excel Application must remain open. If the Excel Application remains open, the code-containing workbook will be opened at the scheduled time and the procedure it triggers will run.ontime needs a specific time to run so if you need to run it after 1 minute you have to do it like this:
if you don't add NOW() it will run at 1 minute past midnight. Or you can also use TIME instead of NOW.Code:Sub RunOnTime() Application.OnTime Now+TimeValue("00:01:00"), "openBK" End Sub
And the workbook, containing the code, must stay open the entire time.
Sub RunOnTime()
Application.OnTime TimeValue("00:01:00"), "openBK"
End Sub
Sub openBk()
If Not WorkbookOpen("End Line Maintenance Dashboard Master.xlsm") Then
Workbooks.Open "O:\QACOM\Production Reports\End Line Maintenance Dashboard Master.xlsm"
End If
End Sub
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
WorkbookOpen = False
On Error GoTo WorkBookNotOpen
If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
WorkbookOpen = True
Exit Function
End If
WorkBookNotOpen:
End Function
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime dTime, "MyMacro", , False
End Sub
Private Sub Workbook_Open()
Application.OnTime Now + TimeV
alue("00:00:30"), "MyMacro"
End Sub
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:30")
Application.OnTime dTime, "MyMacro"
Workbooks.Open "C:\Applications\BookTo Open.xlsm"
End Sub