Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Running a Macro periodically between Time A and Time B

Thanks in advance to anyone who can help me with this.

I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here.. http://www.cpearson.com/excel/ontime.htm and
here.. http://www.ozgrid.com/Excel/run-macro-on-time.htm as well as
many more) and cannot really get my head around the easiest way to do
this.

Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.

It would be much appreciated if anyone could help an amateur.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default Running a Macro periodically between Time A and Time B

I would take another look at Chip's instructions and code:
http://www.cpearson.com/excel/OnTime.aspx

Then if you have specific questions about that code, post back. (Read through
the "stopping a time" section. Ignore the stuff after that.)

And for Chip's code, you can put all that stuff in a single module.

On 09/29/2010 15:09, Kfletch wrote:
Thanks in advance to anyone who can help me with this.

I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here.. http://www.cpearson.com/excel/ontime.htm and
here.. http://www.ozgrid.com/Excel/run-macro-on-time.htm as well as
many more) and cannot really get my head around the easiest way to do
this.

Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.

It would be much appreciated if anyone could help an amateur.


--
Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Running a Macro periodically between Time A and Time B

On Sep 29, 9:05*pm, Dave Peterson wrote:
I would take another look at Chip's instructions and code:http://www.cpearson.com/excel/OnTime.aspx

Then if you have specific questions about that code, post back. *(Read through
the "stopping a time" section. *Ignore the stuff after that.)

And for Chip's code, you can put all that stuff in a single module.

On 09/29/2010 15:09, Kfletch wrote:





Thanks in advance to anyone who can help me with this.


I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here..http://www.cpearson.com/excel/ontime.htmand
here..http://www.ozgrid.com/Excel/run-macro-on-time.htmas well as
many more) and cannot really get my head around the easiest way to do
this.


Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.


It would be much appreciated if anyone could help an amateur.


--
Dave Peterson- Hide quoted text -

- Show quoted text -




OK, you are correct, I made my way into the process a bit and
determined that several of my questions were easily answered.

Now, for the actual code question. In this section for starting (and
stopping) the timer here...

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

I am not sure which portions of this are actual code that needs to
remain, and which portions I need to complete, also what format the
numbers need to be in.
So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
and have it run every 10 minutes between those times, would this
complete the needs of the timer

Sub StartTimer()
RunWhen = Now + TimeSerial(0,0,c600.00)
Application.OnTime EarliestTime:=08:35:00, Procedu=c"My
Macro Name", _
Schedule:=True
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Running a Macro periodically between Time A and Time B

On Sep 30, 12:17*pm, Kfletch wrote:
On Sep 29, 9:05*pm, Dave Peterson wrote:





I would take another look at Chip's instructions and code:http://www.cpearson.com/excel/OnTime.aspx


Then if you have specific questions about that code, post back. *(Read through
the "stopping a time" section. *Ignore the stuff after that.)


And for Chip's code, you can put all that stuff in a single module.


On 09/29/2010 15:09, Kfletch wrote:


Thanks in advance to anyone who can help me with this.


I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here..http://www.cpearson.com/excel/ontime.htmand
here..http://www.ozgrid.com/Excel/run-macro-on-time.htmaswell as
many more) and cannot really get my head around the easiest way to do
this.


Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.


It would be much appreciated if anyone could help an amateur.


--
Dave Peterson- Hide quoted text -


- Show quoted text -


OK, you are correct, I made my way into the process a bit and
determined that several of my questions were easily answered.

Now, for the actual code question. In this section for starting (and
stopping) the timer here...

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

I am not sure which portions of this are actual code that needs to
remain, and which portions I need to complete, also what format the
numbers need to be in.
So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
and have it run every 10 minutes between those times, would this
complete the needs of the timer

* *Sub StartTimer()
* * * *RunWhen = Now + TimeSerial(0,0,c600.00)
* * * *Application.OnTime EarliestTime:=08:35:00, Procedu=c"My
Macro Name", _
* * * * * *Schedule:=True
* *End Sub- Hide quoted text -

- Show quoted text -



After a little more work I have been able to get the macro running,
however I cannot determine where I input the time to start the timer,
and where to stop the timer. See full code below.

Public RunWhen As Double
Public Const cRunIntervalSeconds = 60 ' one minute
Public Const cRunWhat = "SpreadRecordMacro1"
_____________________________________________
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedu=cRunWhat, _
Schedule:=True

End Sub
____________________________________________

Sub SpreadRecordMacro1()
' SpreadRecordMacro1 Macro
' '
"My Macro" .............

StartTimer

End Sub
___________________________________________

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

I have tried to input a time to start, and a time to stop. I must be
inputing in improperly or in the incorrect place. Any advice would be
much appreciated.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default Running a Macro periodically between Time A and Time B

Chip's code is pretty nice.

This line:
Public Const cRunIntervalSeconds = 60
is how you can change the interval.

since you want 600 seconds (10 minutes), you can use:
Public Const cRunIntervalSeconds = 600 '60*10


=========
This is untested, but it did compile.

Option Explicit
'So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
'and have it run every 10 minutes between those times, would this
'complete the needs of the timer
Public RunWhen As Double
Public Const cRunIntervalSeconds As Long = 600 '60 seconds * 10 minutes
Public Const cRunWhat As String = "SpreadRecordMacro1"
Public Const BeginTime As Date = #8:35:00 AM#
Public Const FinishTime As Date = #3:05:00 PM#
Sub Auto_Open()
If Time < BeginTime Then
'wait until that time
Application.OnTime earliesttime:=BeginTime, _
procedu=cRunWhat
Else
'just start it right now????
Application.Run cRunWhat
End If
End Sub
Sub Auto_Close()
'if you're closing the workbook, then
'stop the timer from reopening your workbook
'and running the macro!!
Call StopTimer
End Sub
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedu=cRunWhat, _
Schedule:=True
End Sub
Sub SpreadRecordMacro1()
MsgBox "My Macro could go here"

'600/60/24 is 10 minutes
'so check to see if it's too late to schedule the next
'run
If Time (FinishTime - (cRunIntervalSeconds / 60 / 24)) Then
'don't start it again.
Else
StartTimer
End If
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, procedu=cRunWhat, _
Schedule:=False
End Sub

==============
There's actually a small design flaw/bug in this code. If your times got close
to midnight, then the comparisons could fail when you when want them to.

But I bet that won't affect you. If you do start to work long hours, I'll leave
it to you to fix the problem!


On 09/30/2010 13:01, Kfletch wrote:
On Sep 30, 12:17 pm, wrote:
On Sep 29, 9:05 pm, Dave wrote:





I would take another look at Chip's instructions and code:http://www.cpearson.com/excel/OnTime.aspx


Then if you have specific questions about that code, post back. (Read through
the "stopping a time" section. Ignore the stuff after that.)


And for Chip's code, you can put all that stuff in a single module.


On 09/29/2010 15:09, Kfletch wrote:


Thanks in advance to anyone who can help me with this.


I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here..http://www.cpearson.com/excel/ontime.htmand
here..http://www.ozgrid.com/Excel/run-macro-on-time.htmaswell as
many more) and cannot really get my head around the easiest way to do
this.


Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.


It would be much appreciated if anyone could help an amateur.


--
Dave Peterson- Hide quoted text -


- Show quoted text -


OK, you are correct, I made my way into the process a bit and
determined that several of my questions were easily answered.

Now, for the actual code question. In this section for starting (and
stopping) the timer here...

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

I am not sure which portions of this are actual code that needs to
remain, and which portions I need to complete, also what format the
numbers need to be in.
So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
and have it run every 10 minutes between those times, would this
complete the needs of the timer

Sub StartTimer()
RunWhen = Now + TimeSerial(0,0,c600.00)
Application.OnTime EarliestTime:=08:35:00, Procedu=c"My
Macro Name", _
Schedule:=True
End Sub- Hide quoted text -

- Show quoted text -



After a little more work I have been able to get the macro running,
however I cannot determine where I input the time to start the timer,
and where to stop the timer. See full code below.

Public RunWhen As Double
Public Const cRunIntervalSeconds = 60 ' one minute
Public Const cRunWhat = "SpreadRecordMacro1"
_____________________________________________
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedu=cRunWhat, _
Schedule:=True

End Sub
____________________________________________

Sub SpreadRecordMacro1()
' SpreadRecordMacro1 Macro
''
"My Macro" .............

StartTimer

End Sub
___________________________________________

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

I have tried to input a time to start, and a time to stop. I must be
inputing in improperly or in the incorrect place. Any advice would be
much appreciated.


--
Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Running a Macro periodically between Time A and Time B

On Sep 30, 7:39*pm, Dave Peterson wrote:
Chip's code is pretty nice.

This line:
Public Const cRunIntervalSeconds = 60
is how you can change the interval.

since you want 600 seconds (10 minutes), you can use:
Public Const cRunIntervalSeconds = 600 '60*10

=========
This is untested, but it did compile.

Option Explicit
'So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
'and have it run every 10 minutes between those times, would this
'complete the needs of the timer
Public RunWhen As Double
Public Const cRunIntervalSeconds As Long = 600 '60 seconds * 10 minutes
Public Const cRunWhat As String = "SpreadRecordMacro1"
Public Const BeginTime As Date = #8:35:00 AM#
Public Const FinishTime As Date = #3:05:00 PM#
Sub Auto_Open()
* * *If Time < BeginTime Then
* * * * *'wait until that time
* * * * *Application.OnTime earliesttime:=BeginTime, _
* * * * * * *procedu=cRunWhat
* * *Else
* * * * *'just start it right now????
* * * * *Application.Run cRunWhat
* * *End If
End Sub
Sub Auto_Close()
* * *'if you're closing the workbook, then
* * *'stop the timer from reopening your workbook
* * *'and running the macro!!
* * *Call StopTimer
End Sub
Sub StartTimer()
* * *RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
* * *Application.OnTime earliesttime:=RunWhen, procedu=cRunWhat, _
* * * * *Schedule:=True
End Sub
Sub SpreadRecordMacro1()
* * *MsgBox "My Macro could go here"

* * *'600/60/24 is 10 minutes
* * *'so check to see if it's too late to schedule the next
* * *'run
* * *If Time (FinishTime - (cRunIntervalSeconds / 60 / 24)) Then
* * * * *'don't start it again.
* * *Else
* * * * *StartTimer
* * *End If
End Sub
Sub StopTimer()
* * *On Error Resume Next
* * *Application.OnTime earliesttime:=RunWhen, procedu=cRunWhat, _
* * * * *Schedule:=False
End Sub

==============
There's actually a small design flaw/bug in this code. *If your times got close
to midnight, then the comparisons could fail when you when want them to.

But I bet that won't affect you. *If you do start to work long hours, I'll leave
it to you to fix the problem!

On 09/30/2010 13:01, Kfletch wrote:





On Sep 30, 12:17 pm, *wrote:
On Sep 29, 9:05 pm, Dave *wrote:


I would take another look at Chip's instructions and code:http://www.cpearson.com/excel/OnTime.aspx


Then if you have specific questions about that code, post back. *(Read through
the "stopping a time" section. *Ignore the stuff after that.)


And for Chip's code, you can put all that stuff in a single module.


On 09/29/2010 15:09, Kfletch wrote:


Thanks in advance to anyone who can help me with this.


I have a simple excel macro written and want it to run every so often
(5 min / 10 min / 1 hour not sure yet) between the hours of 8:35:00 am
and 3:05:00 pm. I have been digging through related topics regarding
the OnTime method (here..http://www.cpearson.com/excel/ontime.htmand
here..http://www.ozgrid.com/Excel/run-macr...me.htmaswellas
many more) and cannot really get my head around the easiest way to do
this.


Most of my confusion comes from where to write the code whether it be
in my current macro module, or in new modules / where and how the
sub's and sub of sub's go. Also I have trouble determining what is
actual code and what is a field I must define from much of the
information that is out there.


It would be much appreciated if anyone could help an amateur.


--
Dave Peterson- Hide quoted text -


- Show quoted text -


OK, you are correct, I made my way into the process a bit and
determined that several of my questions were easily answered.


Now, for the actual code question. In this section for starting (and
stopping) the timer here...


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


I am not sure which portions of this are actual code that needs to
remain, and which portions I need to complete, also what format the
numbers need to be in.
So If I want the macro to run at 8:35:00 am and stop at 15:05:00 pm
and have it run every 10 minutes between those times, would this
complete the needs of the timer


* * Sub StartTimer()
* * * * RunWhen = Now + TimeSerial(0,0,c600.00)
* * * * Application.OnTime EarliestTime:=08:35:00, Procedu=c"My
Macro Name", _
* * * * * * Schedule:=True
* * End Sub- Hide quoted text -


- Show quoted text -


After a little more work I have been able to get the macro running,
however I cannot determine where I input the time to start the timer,
and where to stop the timer. See full code below.


Public RunWhen As Double
Public Const cRunIntervalSeconds = 60 ' one minute
Public Const cRunWhat = "SpreadRecordMacro1"
_____________________________________________
Sub StartTimer()
* * * RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
* * *Application.OnTime EarliestTime:=RunWhen, Procedu=cRunWhat, _
* * * * *Schedule:=True


End Sub
____________________________________________


Sub SpreadRecordMacro1()
' SpreadRecordMacro1 Macro
''
* * *"My Macro" .............


* * *StartTimer


End Sub
___________________________________________


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


I have tried to input a time to start, and a time to stop. I must be
inputing in improperly or in the incorrect place. Any advice would be
much appreciated.


--
Dave Peterson- Hide quoted text -

- Show quoted text -



Dave;

I am very appreciative of your help. Your code is working wonderfully
for now and I will watch it a few days to confirm that it is stopping
and starting properly.

The only problem i am running into now is that I run about 5 different
excel models on my screens during the day. I switch back and forth
between them and just realized that the "my macro" (a simple copy :
Paste Special command) is running in whatever excel model I happen to
be in. What is the exact code and procedure (specifically for this
series of sub and sub macros) to ensure that the macro only runs in
the workbook I have the macro built in? In looking at the current
code, it would seem the time variables would not need to be workbook
specific, just "my Macro". I assume I would just need to add a line
before the first line in the "my macro" code that essentially states
"go to - a specific file, run the macro". I dont mind if it forces the
workbook I am working in to become inactive since, from my research,
having it work in the background on an inactive workbook seems
troublesome.

Best Regards
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default Running a Macro periodically between Time A and Time B

I'm confused about your question.

If the macros in the correct workbook are not running, then make sure you use
nice unique names for each of the macros. It'll make running the correct one
easier.

If you mean that the correct macro runs, but it runs against the wrong
worksheet(s), then make sure you qualify the worksheets.

Instead of using:
worksheets("Sheet1").range("A1:b99").copy
worksheets("Sheet2").range("A1").pastespecial _
paste:=xlpastevalues

make sure you refer to them with something like:

ThisWorkbook.worksheets("Sheet1").range("A1:b99"). copy
ThisWorkbook.worksheets("Sheet2").range("A1").past especial _
paste:=xlpastevalues

ThisWorkbook will refer to the workbook that owns the code.



On 10/01/2010 09:12, Kfletch wrote:
<<snipped


Dave;

I am very appreciative of your help. Your code is working wonderfully
for now and I will watch it a few days to confirm that it is stopping
and starting properly.

The only problem i am running into now is that I run about 5 different
excel models on my screens during the day. I switch back and forth
between them and just realized that the "my macro" (a simple copy :
Paste Special command) is running in whatever excel model I happen to
be in. What is the exact code and procedure (specifically for this
series of sub and sub macros) to ensure that the macro only runs in
the workbook I have the macro built in? In looking at the current
code, it would seem the time variables would not need to be workbook
specific, just "my Macro". I assume I would just need to add a line
before the first line in the "my macro" code that essentially states
"go to - a specific file, run the macro". I dont mind if it forces the
workbook I am working in to become inactive since, from my research,
having it work in the background on an inactive workbook seems
troublesome.

Best Regards


--
Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Running a Macro periodically between Time A and Time B

On Oct 1, 11:53*am, Dave Peterson wrote:
I'm confused about your question.

If the macros in the correct workbook are not running, then make sure you use
nice unique names for each of the macros. *It'll make running the correct one
easier.

If you mean that the correct macro runs, but it runs against the wrong
worksheet(s), then make sure you qualify the worksheets.

Instead of using:
worksheets("Sheet1").range("A1:b99").copy
worksheets("Sheet2").range("A1").pastespecial _
* * paste:=xlpastevalues

make sure you refer to them with something like:

ThisWorkbook.worksheets("Sheet1").range("A1:b99"). copy
ThisWorkbook.worksheets("Sheet2").range("A1").past especial _
* * paste:=xlpastevalues

ThisWorkbook will refer to the workbook that owns the code.

On 10/01/2010 09:12, Kfletch wrote:
<<snipped







Dave;


I am very appreciative of your help. Your code is working wonderfully
for now and I will watch it a few days to confirm that it is stopping
and starting properly.


The only problem i am running into now is that I run about 5 different
excel models on my screens during the day. I switch back and forth
between them and just realized that the "my macro" (a simple copy :
Paste Special command) is running in whatever excel model I happen to
be in. What is the exact code and procedure (specifically for this
series of sub and sub macros) to ensure that the macro only runs in
the workbook I have the macro built in? In looking at the current
code, it would seem the time variables would not need to be workbook
specific, just "my Macro". I assume I would just need to add a line
before the first line in the "my macro" code that essentially states
"go to - a specific file, run the macro". I dont mind if it forces the
workbook I am working in to become inactive since, from my research,
having it work in the background on an inactive workbook seems
troublesome.


Best Regards


--
Dave Peterson- Hide quoted text -

- Show quoted text -


Your second part answered my question. I had to have the proper
workbook go "active" prior to running the macro. Everything seems to
be working fine now. Thank you very much for your help!
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel files, periodically, taking a long time to save or open mavio Excel Discussion (Misc queries) 0 October 16th 09 04:43 PM
Macro running time Pradip Jain Excel Programming 2 April 22nd 05 01:50 PM
Running a macro at a certain time Tam Excel Discussion (Misc queries) 3 March 2nd 05 02:50 PM
All time running macro gocush[_29_] Excel Programming 2 January 12th 05 03:19 PM
Running a macro at a certain time Dave Peterson[_3_] Excel Programming 0 October 17th 03 01:49 AM


All times are GMT +1. The time now is 04:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"