Schedule a macro with user defined settings
Matt,
I've got a workable version here.
Place this in a module:
Public nexttime As Double
Public interval As Double
Public start As Date
Public endtm As Date
Public Const RunWhat = "Perform_Tasks"
Sub scheduler()
interval = TimeSerial(0, 0, Sheet1.Range("B2"))
start = Sheet1.Range("B3")
endtm = Sheet1.Range("B4")
nexttime = Now + interval
If Sheet1.Range("B1") = "Enabled" Then
If Time = start And Time + interval < endtm Then
Application.OnTime nexttime, RunWhat
Else: Application.OnTime start, RunWhat
End If
End If
End Sub
Sub endscheduler()
On Error Resume Next
Application.OnTime lasttime, RunWhat, , False
Application.OnTime nexttime, RunWhat, , False
End Sub
Sub Perform_Tasks()
lasttime = nexttime
scheduler
Sheet1.Cells(2, 4) = Now
End Sub
And put this is the ThisWorkbook module.
Private Sub Workbook_Open()
scheduler
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
endscheduler
End Sub
If you've already got a Workbook_Open or Workbook_BeforeClose event just add
the lines to those. The interval in B2 is in minutes and B3/B4 should be
times not dates. I'm wasn't sure how you wanted the macro to start so I put
it in the workbook_open procedure. You could also put a call to it from a
Worksheet_Change event for cell B1.
If you've got any questions I'd be glad to assist.
Mike
"Matt" wrote:
I would like to run a macro, "Perform_Tasks", on a scheduled basis. I would
like users to be able to select certain criteria for this macro, as such:
A B C....
1 Enable/Disable: Enabled
2 Interval: 15
3 Begin Time: 07:00
4 End Time: 13:00
B1:B4 would be user changable values and would dictate how often the macro
kicks off automatically, if enabled. (B1 is a toggle switch between Enable or
Disabled).
Any suggestions?
Matt
|