Start and Stop on Application.Ontime...

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
i am running this command that (i think) runs my Macro every 15 minutes...


- Application.OnTime Now + TimeValue("00:15:00"), "Index_Analysis" -


But i cant stop it... What command can I use to start it at 9:30 and end it at 4:00 ?

thanks..
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this. Note that you must specify the start and end times in 24 hour format - I assume your 4:00 is really 16:00; if it isn't, the code handles the case where the end time is earlier than the start time by assuming the time range spans midnight. Run StartTimer to start the scheduling.
Code:
Option Explicit

Public RunWhen As Double
Public Const cStartTime = "09:30:00"
Public Const cEndTime = "16:00:00"
Public Const cRunInterval = "00:15:00"
Public Const cRunWhat = "Index_Analysis"  'name of procedure to run


Public Sub StartTimer()
    'If current time is earlier than start time, schedule the macro to start today, otherwise the start time has already passed so start
    'it tomorrow
    
    If Now < Int(Now) + TimeValue(cStartTime) Then
        RunWhen = Int(Now) + TimeValue(cStartTime)
    Else
        RunWhen = Int(Now) + 1 + TimeValue(cStartTime)
    End If
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub

Public Sub RestartTimer()
    Dim d As Integer
    
    'If end time is earlier than start time then time range spans midnight so add 1 day to end time to make it occur tomorrow
    
    d = 0
    If TimeValue(cEndTime) < TimeValue(cStartTime) Then d = 1

    'If current time is earlier than end time, reschedule the macro to run again after the time interval, otherwise stop the timer

    If Now < Int(Now) + d + TimeValue(cEndTime) Then
        RunWhen = Now + TimeValue(cRunInterval)
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
    Else
        StopTimer
    End If
End Sub

Public Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
    MsgBox cRunWhat & " stopped"
End Sub


Sub Index_Analysis()
        
    'Call StartTimer to schedule the procedure again
    RestartTimer
    
    'Put your code here
    '==================
      
End Sub
 
Upvote 0
Err, why did you start a new thread on the same issue? Here:

http://www.mrexcel.com/forum/showthread.php?t=324569

Please stick to your original thread (this one) so we can see what you've tried and any help you've been given, all in one place.

In answer to your other thread, you would just use:

Code:
Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub
 
Upvote 0
sorry about that, I thought it was 2 different issues since I did find the answer to my original post but then couldn't get it to run fully..but makes sense to keep it at the same place....

anyway, sorry but your response was a little vague, should I keep ALL my subs() ? (listed in my other post, good reason to keep it together) I have 3 timer private subs should they stay go? thanks this method is confusing to me...
 
Upvote 0
should I keep ALL my subs() ? (listed in my other post, good reason to keep it together) I have 3 timer private subs should they stay go?
No, scrap your subroutines (why have you got 2 Workbook_Open subroutines?) and replace them with the code in post no. 3 (which goes in the ThisWorkbook module).

Put the code in post no. 2 in a new separate standard module (not a class module or a sheet module). This code includes a 'stub' for your own Index_Analysis() procedure, which can stay there or you can put it in its own standard module instead. Just make sure that the Index_Analysis subroutine includes the statement:

RestartTimer

If you put this as the first statement, the code will run exactly every 15 minutes. If however you put it as the last statement, there will be some 'creep', depending on how long the Index_Analysis code takes to run.
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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