Quick Application.OnTime question...

a-drift

New Member
Joined
Oct 13, 2003
Messages
28
Hello,

I have an Excel file that runs untouched 24/7, and I want it to execute a macro every day at 2:47PM. However, the macro is also automatically executed when the date changes at midnight and I don't understand why or how to fix it. I have the code pasted below.


Public cRunWhen As Date
Private Sub Workbook_Open()

cRunWhen = Sheets("E-mailTab").Range("A21").Text ' (A21 ="2:47PM")
Application.OnTime TimeValue(cRunWhen), "CDO_Mail_Small_Text"

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Presumably CDO_Mail_Small_Text reschedules the macro. What code does it use?
 
Upvote 0
Thanks for the reply,

I don't have the CDO_Mail_Small_Text reschedule the macro. I 'assumed' this wasn't necessary because I don't have the Application.OnTime using a date, just a time.

What code would I need to add at the end of the CDO_Mail_Small_Text macro so that when it executes today at 2:47PM, it will reschedule the Application.OnTime for 2:47PM tomorrow? Will this fix also prevent it from automatically executing at midnight tonight?
 
Upvote 0
You can specify an exact date & time to run (say in 24 hours) with:

Code:
Application.OnTime Date + 1 + TimeValue("14:47:00"),"Your Macro Name Here"

Date + 1 returns tomorrows date.
 
Upvote 0
I'm sorry, but I just need to get a bit clearer on your reply and what this means:

So... then the Application.OnTime DOES assume today's date even when no date parameter is supplied to it (like in my original post)?

Also... then it is possible that the Application.OnTime can be reset for tomorrow (like in your last post) within the CDO_Mail_Small_Text macro, which is in a standard module, even though the original Application.OnTime is set in the Public module?
 
Upvote 0
Unless you are rescheduling the macro or reopening the workbook, your macro should only run once. I think we need to see what the called macro does.
 
Upvote 0
So... then the Application.OnTime DOES assume today's date even when no date parameter is supplied to it (like in my original post)?

That is certainly what the Help file suggests. I have only ever used OnTime with a specific future time, however.

Also... then it is possible that the Application.OnTime can be reset for tomorrow (like in your last post) within the CDO_Mail_Small_Text macro, which is in a standard module, even though the original Application.OnTime is set in the Public module?

Yes. When you say Public Module, do you mean the ThisWorkbook module? OnTime can be scheduled from a standard module.
 
Upvote 0
Yes, sorry, ThisWoorkbook, not Public.

I have inserted the additional Application.OnTime statement to the end of my CDO_Mail_Small_Text macro in my standard module, and will wait to see what happens at midnight tonight and at 2:47PM tomorrow. Here's what I added:

Application.OnTime Date + 1 + TimeValue(Sheets("E-mailTab").Range("A21").Text), "CDO_Mail_Small_Text"

I'll post again with what happens.

Thank you very much for all your help.
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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