Can A Macro Start A Closed Excel Workbook?

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,196
Office Version
  1. 2010
Platform
  1. Windows
Scheduler will not work on our network. Can a macro open a closed workbook? Thanks
 
I haven't used "OnTime" much (I usually use Windows Scheduler).
In your code, it looks like you have it set to 1:00 AM, so it wouldn't run until then.
Are you changing the time in the code so that the time it is supposed to run passes?
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
ontime needs a specific time to run so if you need to run it after 1 minute you have to do it like this:
Code:
Sub RunOnTime()
    Application.OnTime Now+TimeValue("00:01:00"), "openBK"
End Sub
if you don't add NOW() it will run at 1 minute past midnight. Or you can also use TIME instead of NOW.

And the workbook, containing the code, must stay open the entire time.
 
Last edited:
Upvote 0
ontime needs a specific time to run so if you need to run it after 1 minute you have to do it like this:
Code:
Sub RunOnTime()
    Application.OnTime Now+TimeValue("00:01:00"), "openBK"
End Sub
if you don't add NOW() it will run at 1 minute past midnight. Or you can also use TIME instead of NOW.

And the workbook, containing the code, must stay open the entire time.
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.
 
Upvote 0
Maybe I'm not doing this right. I have all the code in a workbook that will stay open and all in a module. This workbook will stay open 24/7. I would like to open the workbook at midnight and at 6 am. I don't understand how the time is set. Does Some of this code need to be in the workbook that will be opened? Thanks
 
Upvote 0
The module Joe4 posted in post #9 should open the workbook at one minute past midnight provided you run the sub RunOnTime prior to that time. You need another ontime procedure to open the workbook at 6 AM. If the workbook is already open you may get a run time error or at least an alert will be displayed. You can avoid either by using a simple function to test for the workbook being open. Something like this:
Code:
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
 
Upvote 0
Thanks for all the help and suggestions. I id fin this that will open the work book every what ever its set at. Thanks

Code:
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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top