Schedule time function VBA

Muhammad_Usman

Active Member
Joined
Dec 9, 2015
Messages
489
Hello Every One,

I have following code which is working perfectly.

i want some changes

1 = Code start on 10 am
2 = Run Every 15 minutes
3 = and close on 7 pm

on dialy basis'

Sub RunEveryFive()
Windows("Book1.xlsx.xlsm").Activate
ThisWorkbook.FollowHyperlink Range("H2").Value
Application.OnTime Now + TimeValue("00:15:00"), "RunEveryFive"
Windows("Book1.xlsx.xlsm").Activate
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
two versions below:
VBA Code:
'# SUB version
Sub RunEveryFive()

    Windows("Book1.xlsx.xlsm").Activate
    ThisWorkbook.FollowHyperlink Range("H2").Value
    Call schedule_next_run("RunEveryFive")
    Windows("Book1.xlsx.xlsm").Activate
End Sub

Sub schedule_next_run(proc_name As String)
    Static start_time, end_time, run_interval, next_run_time
    If IsNull(start_time) Then start_time = TimeValue("10:00:00")
    If IsNull(end_time) Then end_time = Date + TimeValue("19:00:00")
    If IsNull(run_interval) Then run_interval = TimeValue("00:15:00")

    If Time > end_time Then
        next_run_time = (Date + 1) + start_time
    ElseIf Time + run_interval > end_time Then
        next_run_time = Date + end_time
    Else
        next_run_time = Now + run_interval
    End If
    Application.OnTime next_run_time, proc_name
    next_run_time = Null
End Sub

VBA Code:
'# FUNCTION version
Sub RunEveryFive()

    Windows("Book1.xlsx.xlsm").Activate
    ThisWorkbook.FollowHyperlink Range("H2").Value
    Application.OnTime next_run_time, "RunEveryFive"
    Windows("Book1.xlsx.xlsm").Activate
End Sub

Function next_run_time()
    Dim start_time, end_time, run_interval
    start_time = TimeValue("10:00:00")
    end_time = Date + TimeValue("19:00:00")
    run_interval = TimeValue("00:15:00")

    If Time > end_time Then
        next_run_time = (Date + 1) + start_time
    ElseIf Time + run_interval > end_time Then
        next_run_time = Date + end_time
    Else
        next_run_time = Now + run_interval
    End If

End Function
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,307
Members
449,218
Latest member
Excel Master

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