View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Stop all macros in worksheet when left

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