Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Time Interval to run at same flow when open in between of Start time and end time

0

Hi Experts,

Hope everyone is doing well.

I am looking for the time interval to run at the same flow even when the excel open in middle of Start time and end time.

For example:

Start time is 9:00 AM and time interval  is 5 min

If the excel workbook is open at 10:03:00 AM, Is it possible the time interval to run at 10:05:00 AM (follow the same time interval which is set from start like 9:05, 9:10, 9:15, etc) rather at 10:08:00 AM (time interval calculate from excel is open). 

Attached excel sheet for your ready reference.

Can I request for your expert guidance solution.

I would really appreciate your valuable response.

Regards,

Answer
Discuss

Answers

0
Selected Answer

Sunil

You need to three things to do this:

  1. To use the Workbook-Open event
  2. A check of the notes from Microsoft on Application.OnTime method (Excel), specifically the bit about running procedures at a specfic time
  3. Use the MROUND worksheet function to round the time to fixed interval (like 10:05 say)

plus a bit of logic!

In the attached file, the first bit calls two macros from Module 1 and is just:

Private Sub Workbook_Open()

SetStartTime 'initialise
TimerLogic 'set timer to run c.f. start and end times
MsgBox "Timer running; macro next due to run at " & NextTime 'remind user

End Sub
The MROUND function is available in Excel365 and rounds a number to the nearest stated multiple e.g. =MROUND(10, 3) rounds 10 to the nearest multiple of 3 and would give the result 9. It can be used for time too (as here, but best that the multiple is a factor of 60 minutes so events are at the same time past the hour each time).

In the code below (from Module 1), the results will appear in the Intermediate pane of VB Explorer. The key bit is the TimerLogic module which will set the timer for 09:00hrs today (in you case) if the workbook is opened before then, at 5 minute intervals thereafter but after the StopTime (19:00hrs for you) it will set the start and stop times to the next day (if you leave the file open).

You'll need to remove the lines starting Debug.Print... and with replace with code to do what you want:

Public StartTime As Date, StopTime As Date, NextTime As Date, Interval As Date
Sub TimerLogic()

Select Case Now
    Case Is < StartTime 'if before start time, set as start time
        NextTime = StartTime
    Case Is >= StopTime 'if after stop time
        StartTime = StartTime + 1 'set start and stop times for tomorrow tomorrow
        StopTime = StopTime + 1 'run timer for tomorrow
        NextTime = StartTime
    Case Else 'if between times
        NextTime = Now + 0.6 * Interval 'add 60% of interval (to be rounded to next later interval)

End Select

'round time to nearest next interval time
NextTime = Application.WorksheetFunction.MRound(NextTime, Interval)

Debug.Print "Next time set at: " & Now '### to demo
Debug.Print "Due (with interval and rounding)= " & NextTime '### to demo

SetTimer (NextTime)

End Sub
Sub SetTimer(NextTime As Date)

Application.OnTime NextTime, "MyMacro"

End Sub

Sub SetStartTime()

StartTime = Date + TimeValue("09:00:00")
StopTime = Date + TimeValue("19:00:00")
Interval = TimeValue("00:05")

End Sub

Sub MyMacro()
Debug.Print "Done: " & Now
TimerLogic

End Sub


Sub StopTimer()
On Error Resume Next
Tstop = Now + TimeValue("00:00:10")
Application.OnTime Earliesttime:=Tstop, Procedure:="MyMacro", Schedule:=False

End Sub
Provided you have Excel 365, this should work well. If you're using an earlier version, use these lines instead of MROUND:
'round time to nearest next interval time

NextTime = Application.WorksheetFunction.RoundUp((NextTime - Int(NextTime)) / TimeValue("00:05:00"), 0) * Interval
the disadvantage being that the divisor has to be written as TimeValue("00:05:00") rather than Interval (for some reason!).

When I set the constants to 

StartTime = Date + TimeValue("13:50:00")
StopTime = Date + TimeValue("14:15:00")
Interval = TimeValue("00:05")
then the results were (from start to waiting for the macro to run the next day):
Next time set at: 07/09/2021 13:49:18
Due (with interval and rounding)= 07/09/2021 13:50:00
Done: 07/09/2021 13:50:00
Next time set at: 07/09/2021 13:50:00

(etc. until....)
Done: 07/09/2021 14:15:00
Next time set at: 07/09/2021 14:15:00
Due (with interval and rounding)= 08/09/2021 13:50:00
Hope this helps.
Discuss

Discussion

  Thank you John for your quick response and time given for the answer.
Very much Appreciated.
Further, I have gone through the link, but i am looking for something else.
The question is bit complicated to understand, but Let me try to make you understand.
The code is below mention which means The Start time is 9:00 AM to end time 7:00 PM and time interval is every 5 min.
I want to convey that, if I open the excel workbook in between the time zone i.e. between 9 AM and 7 PM. Let say, I open excel at 10:03:00 AM, Now, I am looking for the time interval to follow the same duration of every 5 Min which was taken from start i.e. from 9 AM like interval at 9:05, 9:10, 9:15, etc and after open of excel at 10:03:00 AM, the next interval to be at 10:05:00 AM by following the every 5 min interval from Start at 9 AM rather giving me result time interval at 10:08:00 AM as normal (from open the excel at 10:03:00 AM).
Same i tried to convey in the initial (Main) question.
I sincerely request you please understand the question and help me with your expert solution. Since, my last various experience, you are truly a excel genius i came across. I thank you very much for each and every solution you provided for my question.

 
SunilA (rep: 58) Sep 1, '21 at 8:21 am
Sub SetTimer()
 If Now < (Date + 0.375) Or Now > (Date + 0.791666666666667) Then 
    StopTimer 
     Else
    Interval = Now + TimeValue("00:05:00")      
    Application.OnTime Interval, "MyMacro"      
 End If
 End Sub
SunilA (rep: 58) Sep 1, '21 at 8:21 am
Sunil, as I said, you need to work this out (I understand your problem,know how to do it but don't have time and you need to create a solution rather than rely on us writing your code). The "fixed time" is the clue but not the answer- you need to think how to combine it with intervals.
John_Ru (rep: 6142) Sep 1, '21 at 8:24 am
Hi John,
Thank you very much for your input.
Based on your input, I have tried below couple of combination, but unsuccessful.
I am failing to find the solution.
I sincerely request you please please help me with your expertise knowledge, whenever you have time.

If Now < (Date + 0.375) Or Now > (Date + 0.791666666666667) Then 'test if time is outside working hours
    StopTimer 'stop the timer (if running)
 
    Else
    Interval = Now < (Date = 0.375) + TimeValue("00:00:10")      ' Set your interval here
    Application.OnTime Interval, "MyMacro"      ' name the time & macro to run
 
End If
 
End Sub
 
If Now < (Date + 0.375) Or Now > (Date + 0.791666666666667) Then 'test if time is outside working hours
    StopTimer 'stop the timer (if running)
 
    Else
    Interval = Now + TimeValue("00:00:10")      ' Set your interval here
    Application.OnTime TimeValue("9:00:00"), "MyMacro", TimeValue("3:15:00"), True    ' name the time & macro to run
 
End If
 
End Sub


Regards,
SunilA (rep: 58) Sep 2, '21 at 5:27 am
Hi John,
Please help.
Regards
SunilA (rep: 58) Sep 6, '21 at 8:12 am
Sunil- see revised answer
John_Ru (rep: 6142) Sep 7, '21 at 10:07 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login