Schedule Macros with Application.OnTime with looped in variable times

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Good morning all, today I would like to share a solution. I needed VB macros to run automatically using variable times that have been read in from a range. All of the information on the net only talks about using Application.OnTime with a set time or a set interval. My solution, reads in the scheduled time from a range and then runs the macro according to that time.
Using two macros as follows and most importantly a cell value to keep track of row numbers (this is the important part), everything works. I hope this can help somebody.

VBA Code:
Sub SetOnTime2()
With Sheet1
        'Read in the row value
        i = .Range("A2").Value
        'set the UNQUE time value to run the macro
        ScheduledTime = .Range("F" & i).Value
        Application.OnTime ScheduledTime, "MyCode2"
End With
End Sub


Sub MyCode2()
With Sheet1
'Find last value
irow = .Range("F" & Rows.Count).End(xlUp).Row

    If .Range("A2").Value <= irow Then
        i = .Range("A2").Value
        'Output the Results
        .Range("H" & i).Value = "Time is: " & Format(ScheduledTime, "hh:mm:ss")

            If i = irow Then
                MsgBox "Code Stopped"
                'Reset Starting Row value
                .Range("A2").Value = 3
                Exit Sub
            Else
                'write out the new row value
                .Range("A2").Value = i + 1
            End If
        'call the first macro to read the next time value
        Call SetOnTime2
        Else

    End If
End With
End Sub

This is an example of the Results.

The label "Start Row " is in Cell A2.

Start RowScheduled TimeUnique TimeResult
3​
06:57:00​
06:57:00​
Time is: 06:57:00
06:57:01​
06:57:01​
Time is: 06:57:01
06:57:02​
06:57:02​
Time is: 06:57:02
06:57:03​
06:57:03​
Time is: 06:57:03
06:57:04​
06:57:04​
Time is: 06:57:04
06:57:05​
06:57:05​
Time is: 06:57:05
06:57:06​
06:57:06​
Time is: 06:57:06
06:57:06​
06:57:10​
Time is: 06:57:10
06:57:06​
06:57:11​
Time is: 06:57:11
06:57:06​
06:57:12​
Time is: 06:57:12
06:57:10​
06:57:13​
Time is: 06:57:13
06:57:11​
06:57:18​
Time is: 06:57:18
06:57:12​
06:57:19​
Time is: 06:57:19
06:57:13​
06:57:20​
Time is: 06:57:20
06:57:13​
06:57:21​
Time is: 06:57:21
06:57:13​
06:57:22​
Time is: 06:57:22
06:57:13​
06:57:23​
Time is: 06:57:23
06:57:13​
06:57:18​
06:57:19​
06:57:20​
06:57:21​
06:57:22​
06:57:23​
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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