LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Stop all macros in worksheet when left

There's a worksheet_activate event, too.

But be careful. If you swap to a different workbook, neither of these events
will fire.

You'll want to look at Workbook_WindowActivate and Workbook_WindowDeactivate (in
ThisWorkbook).





dgold82 wrote:

Wow! I leave the office for a couple hours and get tons to digest. Thanks to
all!

I am a real beginner with VBA and didn't realize until one of you mentioned
that there is a worksheet deactivate event. This is what ended up working
(the whole code):

Option Explicit
Private bStopTimer As Boolean

Sub StartTimer45min()
bStopTimer = True
Application.ScreenUpdating = True
Dim tmr As Long
bStopTimer = False
tmr = Timer
Range("k4,e4") = ""

Do
Range("k4").Value = Int(Timer - tmr)
DoEvents
If bStopTimer Then Exit Do
Loop Until Timer tmr + 2700

If bStopTimer Then
Range("k4").Value = 2700
Else
Range("e4") = "Time's Up!"
End If
End Sub

Sub quitTimer()
bStopTimer = True
End Sub

Private Sub Worksheet_Deactivate()
bStopTimer = True
End Sub

The deactivate code right above stops the timer when you change the
worksheet!! I would never have known without all your replies (which are
really fancy and for the most part over my head :-))

I am currently working on figuring out how to add a pause button to the code
above. If I figure that out I could change my deactivate code to the pause
code (instead of stop) and when a user comes back to the worksheet they can
just continue. I'm having a tough time figuring that out so help would be
appreciated in that attempt if you have time.

Thanks!

"Dave Peterson" wrote:

I'll buy into the fact you're smart--too smart to recommend using End <vvbg.

I think I'd stay with the way Chip shows.

Jim Thomlinson wrote:

I tried this and it sort of worked to halt code... On a sheet I added a
command button with the following code...

Dim lng As Long

For lng = 1 To 10000000
DoEvents
Application.StatusBar = lng
Next lng
Application.StatusBar = False

Then in the Deactivate event of thisworkbook I added
Private Sub Workbook_Deactivate()
End
End Sub

if you switch sheets with either
Ctrl+Tab
Window | Select another sheet
It executes the End and halts all code. Interestingly you can not just click
on the sheet in the Task Bar...

Of couse End is generally speaking a bad idea and I do not endorse the use
of this code but I wanted to prove that I'm smart... Best I could manage was
to sort of prove it so I guess I'm sorta smart. ;-)
--
HTH...

Jim Thomlinson

"Dave Peterson" wrote:

Take a look at Chip Pearson's notes:
http://www.cpearson.com/excel/OnTime.aspx

He includes notes on how to kill a pending (not running!) macro.

I'm not sure how you'd stop a running macro via code. (I'm gonna guess it's
impossible--now that should challenge some smart people <vbg.)

dgold82 wrote:

Is there code that I can put in a worksheet module that automatically stops
all the running macros when a user leaves that worksheet? My problem is that
I have a timer macro that runs and if a user leaves that worksheet before it
completely counts down then the screen becomes all garbled until the macro is
stopped.

Thanks!

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


 
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 a clicked on cell being left blank Nicole Answer Excel Worksheet Functions 2 September 6th 07 05:28 AM
How do I stop radio buttons moving left after Printing KoLM#s Excel Discussion (Misc queries) 5 July 19th 07 03:42 PM
How to change the right-to-left worksheet to left-to-right workshe RAMA Excel Discussion (Misc queries) 1 July 4th 05 01:57 PM
Moving Cell up, down, left or right thru Macros Mustafa S N Excel Programming 5 March 21st 05 01:09 PM
Macros - stepping left in a macro Rich H. Excel Programming 2 October 5th 03 06:45 PM


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

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"