Run a macro from another workbook with application.ontime

Justice98

New Member
Joined
Jun 15, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have three workbooks, which have to execute the following code at a certain time.

WORKBOOK1 Code
VBA Code:
Private Sub Workbook_Open()
    MsgBox "The sheets are printed."
    Application.OnTime TimeValue("12:00:00"), "'Workbook1.xlsm'!PrintWB1"
    Application.OnTime TimeValue("16:00:00"), "'Workbook1.xlsm'!PrintWB1"
End Sub

WORKBOOK2 Code
VBA Code:
Private Sub Workbook_Open()
    MsgBox "The sheets are printed."
    Application.OnTime TimeValue("12:00:00"), "'Workbook2.xlsm'!PrintWB2"
    Application.OnTime TimeValue("16:00:00"), "'Workbook2.xlsm'!PrintWB2"
End Sub

WORKBOOK3 Code
VBA Code:
Private Sub Workbook_Open()
    MsgBox "The sheets are printed."
    Application.OnTime TimeValue("12:00:00"), "'Workbook3.xlsm'!PrintWB3"
    Application.OnTime TimeValue("16:00:00"), "'Workbook3.xlsm'!PrintWB3"
End Sub

My problem is that the workbook that is activated is the only one which get printed. The others give the folowwing error: "The macro may not be available in this workbook or all macros may be disabled."
But the macros are enabled in every workbook and they work if I just execute the macro when the workbook is activated. I have tried to add the Application.Run etc. but still can't fix the problem.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The problem is that VBA is waiting, first for 12:00 to come, then for 16:00 to come. In the meantime it can't do anything else, including running other macros.

It will be better to have one macro in some workbook, that runs the printing macro's for each workbook.

You will still have the problem that you ca't access Excel in the waiting time.

You could overcome that by setting up a agent in Windows that runs on the specified times, and opens the workbook that has the macro. This workbook should run the macro on opening and close itself once done.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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