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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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
Thanks for the response. If it would be better in Windows Task Scheduler is that something you could help me with
 
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,215,889
Messages
6,127,592
Members
449,386
Latest member
owais87

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