Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default How to stop a macro

I'd like a macro to stop after 20 seconds, if it has not finished.
What is the easiest way to do this?

Thanks
Dean
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default How to stop a macro

On Apr 3, 3:48*pm, DA wrote:
I'd like a macro to stop after 20 seconds, if it has
not finished. What is the easiest way to do this?


Press ctrl+Break. (On my laptop, it is ctrl+alt+Pause.) That usually
interrupts the procedure and allows you debug or end.

Oh, did you mean programmatically? ;-)

See the following example. Essentially, OnTime is used to call a
procedure after a prescribed amount of time (5 sec, in the example).
When the timeOut function is called, it sets a global variable stopIt
to True. When the main procedure (doit) sees that stopIt is True, the
main procedure exits the loop.

However, this works on if the main procedure yields the CPU
periodically. See the use of DoEvents() in the main procedure. I
imagine that can be costly in execution time.

Also, there are many other conditions that will prevent the timeOut
procedure from running when the timer pops.

See http://www.cpearson.com/excel/OnTime.aspx for another method. I
don't know if avoids any or all of the above pitfalls.

Example....

Private stopIt As Boolean
Private st As Double

Sub doit()
'* see also http://www.cpearson.com/excel/OnTime.aspx
stopIt = False
st = Now
Application.OnTime st + TimeSerial(0, 0, 5), "timeOut"
myst = Timer
Do
'* abort if no timeout after 10 sec
If Timer - myst 10 Then MsgBox "no timeout": Exit Sub

'* must use DoEvents to allow timeOut to run.
'* comment out following line to see what happens
x = DoEvents()
Loop Until stopIt
MsgBox "stopit"
End Sub

Private Sub timeOut()
stopIt = True
et = Now
MsgBox "timeOut" & Format(st, " hh:mm:ss") & _
Format(et, " hh:mm:ss")
End Sub
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
How do I stop this macro? nospaminlich Excel Programming 5 October 31st 05 03:56 PM
Stop running a macro in the middle of a macro gmunro Excel Programming 3 June 9th 05 06:00 PM
stop a macro if it does not contain Wildman Excel Programming 4 May 31st 05 10:11 AM
Macro: With Stop it works. Without Stop it doesn't. Don Wiss Excel Programming 2 October 12th 04 10:49 AM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 09:05 PM.

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

About Us

"It's about Microsoft Excel"