View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Andy Andy is offline
external usenet poster
 
Posts: 414
Default Start/Repeat/Stop Macro at specific time/interval

Excellent! It works perfectly!
Clarification: I should close this file by midnight each night. And, if I
open this file before 830am will it still work the same? As of now, I open
this file before 830am so that it runs while I am at work. Thanks!

"Dave Peterson" wrote:

Ignore that first note--I changed the code but forgot to delete the note. This
fixes a word wrap problem, too:

Option Explicit
Public RunWhen As Double
Public Const cRunIntervalSeconds = 15
Public Const cRunWhat = "The_Sub" ' the name of the procedure to run
Dim FirstTime As Boolean
Sub StartTimer()
If FirstTime Then
RunWhen = TimeSerial(8, 30, 0)
Else
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
End If
Application.OnTime EarliestTime:=RunWhen, _
Procedu=cRunWhat, Schedule:=True
End Sub
Sub The_Sub()

MsgBox "hi " & Time
'''''''''''''''''
' Your Code Here
'''''''''''''''''
' Call StartTimer to schedule the procedure again
If Time TimeSerial(15, 0, 0) Then
'do nothing
Else
StartTimer
End If
End Sub
Sub StopTimer()
'useful for testing to stop any scheduled macro
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, _
Procedu=cRunWhat, Schedule:=False
End Sub
Sub Auto_Open()
FirstTime = True
If Time TimeSerial(18, 30, 0) Then
FirstTime = False
End If
Call StartTimer
FirstTime = False
End Sub

But this still applies:

This does assume that you close excel before midnight and reopen this file the
next day.



Dave Peterson wrote:
<<snipped
--

Dave Peterson