PDA

View Full Version : Solved: macro timer



vzachin
10-25-2008, 11:21 AM
hi,

i need to run a macro automatically every 20 minutes within an 8-hour timeframe. is that possible? if yes, then is there a way to add a button to terminate the macro sooner than the 8-hour time frame?

ideally, i would fire the macro at 6 am, and every 20 minutes, the macro "does stuff" then goes to sleep for another 20 minutes..and continues for 8 hours. and if i want to terminate the macro, click a button


thanks
zach

Bob Phillips
10-25-2008, 12:40 PM
Public nTime As Double

Public Sub StartTimer()

If Now - Int(Now) < Time(14, 0, 0) Then

nTime = Now + Time(0, 20, 0)
Application.OnTime nTime, "StartTimer"
Call myProc
End If
End Sub

Public Sub StopTimer()

Application.OnTime nTime, "StartTimer", , False
End Sub

vzachin
10-25-2008, 01:40 PM
hi bob,

i'm getting a run-time error 13 (type mismatch) here:
If Now - Int(Now) < Time(14, 0, 0) Then

i'm using excel 2003


thanks
zach

GTO
10-25-2008, 02:49 PM
Greetings Zach,

A tiny tweak to Bob's. Just substitute "TimeSerial" for the two places "Time" is...

Hope the weather is nice up there in New York (a 'favorite' Aunt lives up there).

Hope this helps,

Mark

Option Explicit
Public nTime As Double

Public Sub StartTimer()
If Now - Int(Now) < TimeSerial(14, 0, 0) Then
nTime = Now + TimeSerial(0, 20, 0)
Application.OnTime nTime, "StartTimer"
Call myProc
End If
End Sub

Public Sub StopTimer()
Application.OnTime nTime, "StartTimer", , False
End Sub

vzachin
10-25-2008, 07:49 PM
thanks mark & bob for the coding.
it took me a while to figure out TimeSerial(14, 0, 0) = 2PM.
i also found out about TimeValue("2 PM")

ny is windy & stormy tonight but will be in the balmy 60s tomorrow. great weather for your aunt...

thanks again
zach

GTO
10-25-2008, 08:28 PM
Oh no thanks needed for me; Bob's clear/consice coding just astounds me, and I was certainly learning as well.

Thank you for the weather advisement and have a great evening,

Mark