Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have vba execute every 10 minutes?

Tags:

excel

vba

I need to have my macro executed every 10 minutes .

This allows it to work in 10 minutes

sub my_Procedure () 
msgbox "hello world"
end sub

sub test()
Application.OnTime Now + TimeValue("00:00:10"), "my_Procedure"
end sub

But this works only once . How can I have my macro execute every 10 minutes ?

like image 863
Buras Avatar asked Mar 31 '14 21:03

Buras


People also ask

How do I make a macro run every 10 minutes?

TimeValue("00:10:00") run every 10 minutes. TimeValue("00:00:30") run every 30 seconds.

How do I set a timer in VBA?

Use the above and type your code after the code StartingTime = Timer, but before the code MsgBox Timer – StartingTime, i.e., in a green area, you need to enter your code.


2 Answers

Consider:

Public RunWhen As Double
Public Const cRunWhat = "my_Procedure"

Sub StartTimer()
RunWhen = Now + TimeSerial(0, 10, 0)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
     schedule:=True
End Sub

Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliesttime:=RunWhen, _
       procedure:=cRunWhat, schedule:=False
End Sub

Sub my_Procedure()
    MsgBox "hello world"
    Call StartTimer
End Sub

all in a standard module..............be sure to run StopTimer before exiting Excel

NOTE

The "minute" argument in TimeSerial is the second argument.

like image 149
Gary's Student Avatar answered Oct 11 '22 14:10

Gary's Student


You should use this pattern:

Sub my_Procedure()
    MsgBox "hello world"        
    Call test ' for starting timer again
End Sub

Sub test()
    Application.OnTime Now + TimeValue("00:10:00"), "my_Procedure"
End Sub
like image 29
Dmitry Pavliv Avatar answered Oct 11 '22 14:10

Dmitry Pavliv