ontime with end time

eran3185

Board Regular
Joined
Apr 28, 2007
Messages
142
hi

i want to use "ontime".
i want to run this macro from 18:00 , every 1 minute , until 18:30.
the problem with "ontime" that there is only start point , but there is not end point (18:30)

how can i get also the end point ?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Diablo II

Well-known Member
Joined
Sep 28, 2008
Messages
538
like this
Code:
Application.OnTime earliesttime:=Timetorun, _
   procedure:="OnTimeMacro", schedule:=False
 
Upvote 0

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,798
OnTime does have an end point - the LatestTime parameter. Try this, based on http://www.cpearson.com/excel/OnTime.aspx:

Code:
Option Explicit

Public RunWhen As Double
Public Const cRunIntervalSeconds = 60 ' one minute
Public Const cRunWhat = "TheSub"  ' the name of the procedure to run
Public Const cStartTime = "18:00"
Public Const cEndTime = "18:30"

Sub Test()
    StartTimer
End Sub

Sub StartTimer()
    Application.OnTime earliesttime:=TimeValue(cStartTime), Procedure:=cRunWhat, latesttime:=TimeValue(cEndTime), schedule:=True
End Sub

Sub RestartTimer()
    RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
    Application.OnTime earliesttime:=RunWhen, Procedure:=cRunWhat, latesttime:=TimeValue(cEndTime), schedule:=True
End Sub

Sub TheSub()
    ''''''''''''''''''''''''
    ' Your code here
    ''''''''''''''''''''''''
    MsgBox Time
    
    RestartTimer  ' Reschedule the procedure
End Sub
 
Upvote 0

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
Untested...

Based on Chip Pearson's code...
Code:
Option Explicit

Public RunWhen As Date
Public Const StartTime As Date = #6:00:00 PM#, EndTime As Date = #6:30:00 PM#, _
    cRunIntervalSeconds = 60 ' two minutes
Public Const cRunWhat = "TheSub"  ' the name of the procedure to run


Sub SchedTimer()
    If RunWhen = 0 Then RunWhen = StartTime _
    Else RunWhen = RunWhen + TimeSerial(0, 0, cRunIntervalSeconds)
    If RunWhen < EndTime Then _
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
            Schedule:=True
    End Sub


Sub TheSub()
    ''''''''''''''''''''''''
    ' Your code here
    ''''''''''''''''''''''''
    SchedTimer  ' Reschedule the procedure
    End Sub
hi

i want to use "ontime".
i want to run this macro from 18:00 , every 1 minute , until 18:30.
the problem with "ontime" that there is only start point , but there is not end point (18:30)

how can i get also the end point ?
 
Upvote 0

eran3185

Board Regular
Joined
Apr 28, 2007
Messages
142
thank's.
its a great web site :)

i try to use this codes , but it didn't stop ...


i wrote this :

"Option Explicit

Public RunWhen As Double
Public Const cRunIntervalSeconds = 10 ' one minute
Public Const cRunWhat = "TheSub" ' the name of the procedure to run
Public Const cStartTime = "8:10"
Public Const cEndTime = "8:11"

Sub Test()
StartTimer
End Sub

Sub StartTimer()
Application.OnTime earliesttime:=TimeValue(cStartTime), Procedure:=cRunWhat, latesttime:=TimeValue(cEndTime), schedule:=True
End Sub

Sub RestartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, Procedure:=cRunWhat, latesttime:=TimeValue(cEndTime), schedule:=True
End Sub

Sub TheSub()
''''''''''''''''''''''''
' Your code here
''''''''''''''''''''''''
MsgBox Time

RestartTimer ' Reschedule the procedure
End Sub "
 
Upvote 0

Jinspet

New Member
Joined
Mar 10, 2009
Messages
13
Code:
Sub yourSubHere()
  'do something here
  callOnTime
End Sub
 
Sub callOnTime()
    If TimeValue(Now) >= TimeValue("18:00") And TimeValue(Now) <= TimeValue("18:30") Then
       Application.OnTime NOW + TimeValue("00:01:00"), yourSubHere
    End If
End Sub
 
Last edited:
Upvote 0

eran3185

Board Regular
Joined
Apr 28, 2007
Messages
142
hi
there is a problem.
the macro copy from a web site every 15 minutes.
when the macro is running , its copy the data also on another excel files that running same time on my computer.
 
Upvote 0

Forum statistics

Threads
1,191,707
Messages
5,988,198
Members
440,138
Latest member
yanaa

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
Top