Auto run a macro on a specified date

Rupert Bennett

Active Member
Joined
Nov 20, 2002
Messages
271
I would like to run a macro on Dec 31st each year. the workbook will be opened, but not necessarily being used on that day so I would like the macro to run by itself. I can code what functions need to be performed. I just do not know how to write the code for the date to be recognized and then do the tasks. Can anyone help with this, if it is possible and also say what module I would place the code in?
Your help will be greatly appreciated.
Rupert
 
Hi chiello, unfortunately your procedure will not complete the task if the book isnt opened on the 31/12. eg book is opened on 30/12 and then next on 1/1 - the procedure will never run. Thats why you need to check if today is > a target date and then after running the macro alter the target date forward so its only run when its next due rather than multiple times.

regards
Parry
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Ciao parry, what you say is exact at all :oops: :oops:

So I have updated my solution. Is it compliant with Rupert'request? :)

Copy in Thisworkbook code window the routine below:

Private Sub Workbook_Open()
Call TestDayOfYear
End Sub


and in a Module code window what follows:

Public Sub TestDayOfYear()
Dim MyDay As Long, MyMonth As Long
Dim CheckDate As Date, RunIN As Date
'Data acquisition
CheckDate = Now
MyDay = Day(CheckDate)
MyMonth = Month(CheckDate)
'Check of Day of the Year
If MyDay = 31 And MyMonth = 12 Then
'Specify time to last from opening to execution (15 seconds in this example)
RunIN = TimeValue("00:00:15")
Application.OnTime Now + RunIN, "my_Procedure" 'change the name of the macro being called.
Else
'Try again later, e.g. 12 hours
RunIN = TimeValue("12:00:00")
Application.OnTime Now + RunIN, "TestDayOfYear" 'Recursive call
End If
End Sub


Ciao :ROFLMAO:
 
Upvote 0
Ciao chiello. :biggrin:

Its certainly an interesting approach to use the OnTime method and would work as long as the workbook remains open. Unfortunately the OnTime method loses its 'memory' of when to run a procedure next when Excel is closed.

Even though it may not be perfect its certainly a clever way to solve the problem. :cool:

arrivederci,
Parry
 
Upvote 0
Sorry Perry, I don't understand... :eek: :eek:

If I close Excel, I can't run the Macro anyway.
If I open It again, I think the code in Workbook_Open routine 'restores Memory' to the OnTime. Where do I fail?

Thanks for your answer..
 
Upvote 0
Hi, the problems are
1) Its still on running the my_Procedure macro on 31/12 so unless the book is opened on that day you still have problems. I believe you actually need to store a date or else you will have problems with it running multiple times or not at all.

If I open It again, I think the code in Workbook_Open routine 'restores Memory' to the OnTime. Where do I fail?
2) Yes your right, I didnt think it would. I ran an OnTime macro closed the book then immediately re-opened it and it ran as scheduled. I wonder if it would still work after rebooting though. However, if the book isnt open at that time it wont run.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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