VBA - Run at Set Intervals

The Power Loon

New Member
Joined
Feb 7, 2020
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I have a script I want to run every 15 minutes, for 5 hours (20 times). I can't use a loop and wait, because it doesn't factor in the time it takes for the script to run, which can vary and throw off when exactly it starts running in subsequent loops.

Is there a way to set a timer that excludes processing time?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can try using this code.

VBA Code:
 ' if you wanna change the timing, the format is Hours:Minutes:Seconds
       
        Application.OnTime Now + TimeValue("00:00:05")

You'd have to account for each time you looped through the code, but that should add a timer to when you wanna run the code
 
Upvote 0
You'd need a Public variable in a code module (e.g. alertTime) where you'd have code such as
VBA Code:
Public Sub EventMacro()
'do whatever here
End Sub
In workbook.open event:
VBA Code:
Private Sub workbook_Open()
alertTime = Time + TimeValue("00:02:00") 'this adds 2 min. to time workbook opens for first run
Application.OnTime alertTime, "EventMacro"
End Sub
If to be run immediately on wb opening, then your wb open event would call EventMacro or whatever procedure you need to run.
 
Upvote 0
You can try using this code.

VBA Code:
' This Sub is the main program that calls the other programs

Sub Start_Timer()
    Dim Run_Program As Integer
    Run_Program = Range("Action").Value
    If (Run_Program = 1) Then
       
       ' if you wanna change the timming, the format is Hours:Minutes:Seconds
       
        Application.OnTime Now + TimeValue("00:00:05"), "DoSomething"
        
    End If

End Sub

You'd have to account for each time you looped through the code, but that should add a timer to when you wanna run the code
 
Upvote 0
Hi to all.
This could be another solution. When file opens (can become a separate macro) all twenty processes Application.OnTime are created and will execute as per array time:
VBA Code:
Option Explicit
Private Sub Workbook_Open()
    Dim timeArray As Variant
    Dim x      As Long
    timeArray = Array("14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45") '<- change as needed
    For x = LBound(timeArray) To UBound(timeArray)
        Application.OnTime TimeValue(timeArray(x)), "MyScript" '<- where MyScript is your macro
    Next x
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
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