OnTime Application Newbie Question

G

Guest

I have cobbled together Chip Pearson's code for running an macro queries of
Yahoo Finance for stock data (code also borrowed). Here's the basic structure
so far:

Public RunWhen As Double
Public Const cRunIntervalSeconds = 120 ' two minutes
Public Const cRunWhat = "GetData" ' the name of the procedure to run

Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=True
End Sub

Sub GetData()
<my code>
StartTimer
End Sub

There is also another generic piece of code courtesy of Mr. Pearson:

Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat,
Schedule:=False
End Sub

...but it is not currently being called.

I want to 1) GetData (see above) between 9:00 and 15:30 daily only (this
spreadsheet will reside on a dedicated computer, hopefully always on), and 2)
perform other macros at, say, 8:30 and 16:00 daily. I am uncertain how these
last two items get coded. Any help appreciated.
 
B

Bob Phillips

Public RunWhen As Double
Public Const cRunIntervalSeconds = 120 ' two minutes
Public cRunWhat As String ' the name of the procedure to run

Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
If RunWhen - Int(RunWhen) >= TimeSerial(8, 30, 0) And _
RunWhen - Int(RunWhen) <= TimeSerial(16, 0, 0) Then
cRunWhat = "GetData"
Else
cRunWhat = "StartTimer"
End If
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, _
Schedule:=True
End Sub

Sub GetData()
MsgBox "hello" '<my code>
StartTimer
End Sub


Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, _
Schedule:=False
End Sub



--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top