LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Stop my timer

Thank you

I appreciate the time you take to help so many of us.

"Chip Pearson" wrote in message
...

Add a module level variable (before and outside of any procedure)
named Cancel As Boolean and then modify your code to something like


Do While Timer < Start + TotalTimeInMinutes
DoEvents
If Cancel = True Then
' other clean up code
Exit Do
End If
Loop


Then, assign the following code to the command button that is to
cancel the timer:

Public btnCancel_Click()
Cancel = True
End Sub


As Dave Hawley wrote, you can allow the user to use ESC or CTRL BREAK
to break out of the loop. You can incorporate such code with the code
above to get something like


On Error GoTo BreakHander
Application.EnableCancelKey = xlErrorHandler
Do While Timer < Start + TotalTimeInMinutes
DoEvents
If Cancel = True Then
' other clean up code
Exit Do
End If
Loop
' the rest of your code
Exit Sub
BreakHandler:
If Err.Number = 18 Then
' user broke out of loop
Cancel = True
Resume Next
Else
' some other error
End If

The EnableCancelKey setting tells VBA to raise an error 18 when the
user breaks hits break. The error handler block sets the Cancel
variable to True and that will break out of the loop. As an aside, it
is possible to set EnableCancelKey to completely disable the break
key. Use that setting with extreme caution because if your code goes
into an infinitely loop, the only way to get out is CTRL ALT DELETE to
total kill off the Excel application and lose all your unsaved
changes.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




On Sun, 16 May 2010 21:24:39 -0700, "ordnance1"
wrote:

I found this code below on-line (can not remember where) but just
wondering
if someone could tell be how to stop the timer? I want the user to have
the
option to stop the timer if necessary.

Option Explicit
Sub Auto_Open()
'
' AutoRun Macro
' Macro recorded 2/3/2001 by Patrick C. Simonds
'

bSELCTIONCHANGE = False

Events.Enable_Events


Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True

TimeInMinutes = 15 'Timer is set for 180 minutes; change as needed.
If TimeInMinutes 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
Loop
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
'msgbox "This file has been open for " & TotalTime / 60 & " minutes.
You
have 5 minutes to save before Excel closes."
End If

Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
Loop
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
'msgbox "Excel will now close."


ThisWorkbook.Close True

End Sub


 
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
Stop timer. Karthik Excel Programming 7 August 28th 10 04:31 AM
Count Timer Pause and Stop button [email protected] Excel Programming 0 May 12th 10 02:05 PM
Timer w/ Start, Stop, Pause dgold82 Excel Programming 2 April 22nd 09 10:13 PM
How to stop 'Egg Timer' MichaelRobert Excel Programming 3 December 4th 08 09:43 PM
stop timer choice[_2_] Excel Programming 1 October 25th 04 12:29 AM


All times are GMT +1. The time now is 05:35 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"