Run a Macro on the 1st of every month at 7AM

JWilson9608

New Member
Joined
Feb 14, 2019
Messages
2
I have a workbook that is set up to always stay open with a Private Sub Workbook_BeforeClose function. I have written a macro called Save_And_Clear to perform a number of routines on the 1st of the month at 7AM. The Save_And_Clear macro works great but I’m having a hard time figuring out the code for the macro to be called on the 1st of every month at 7AM. A currently have an Application.OnTime Day(Date) = 1, “Save_And_Clear” to run the macro but I’m not sure what will happen on the NEXT 1st of the month because the workbook is not allowed to close. I am also having trouble with the 7AM part of the Sub. In testing, I can make it run on startup by date and I can make it run on startup by time Application.OnTime TiveValue(“07:00:00”), “Save_And_Clear” but I can’t seem to make it run by date AND time. Any help would be greatly appreciated.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I think you'd be better off running the workbook from Windows Task Scheduler, but ...

Code:
Option Explicit

Dim Runtime As Date

Sub Sched()
  If Runtime = 0 Then
    Runtime = DateSerial(Year(Date), Month(Date) + 1, 1) + CDate("7:00")
  Else
    Call myMacro
    Runtime = DateAdd("m", 1, Runtime)
  End If
  Application.OnTime Runtime, "Sched"
End Sub

Sub myMacro()
  ' do stuff
End Sub
 
Upvote 0
I don't use it often enough to tell you off the top of my head, but basically you schedule the workbook to open with the desired recurrence, and code so the macro is run from the Workbook_Open event.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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