VBA Can i make a Emergency Stop button for code?

NessPJ

Active Member
Joined
May 10, 2011
Messages
395
Office Version
  1. 365
Hi all,

Does anyone know if it is possible to make a button on my excel sheet that will instantly stop all routines that are active?

I have a piece of code that also executes at a set interval of # minutes (posted below). This does not seem to stop when i press CTRL+Break. I have to run a routine with "Schedule:=False".

But i would also really like to add Button on my Excel sheet that allows the user to click it in case of 'emergency'. Is this possible?

VBA Code:
    RunWhen = Now + TimeSerial(0, RunInterval, 0)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
    Schedule:=True                  'cRunWhat is a Public Const
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
Try this

VBA Code:
Public Const cRunWhat = "test"
Public RunWhen

Sub test()
  RunInterval = 1   'The number of minutes you want
  RunWhen = Now + TimeSerial(0, RunInterval, 0)                                   'RunWhen is a Public Variable
  Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True   'cRunWhat is a Public Const
End Sub

Sub stopEmergency()
  On Error Resume Next
  Application.OnTime RunWhen, cRunWhat, , False
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
Is there also a way or function to suddenly stop any (sub)routine that is currently being executed?
Each subroutine must have a way to exit.
You can interrupt a running macro by pressing the Break key.

Also check the following:
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,455
Members
417,025
Latest member
MusterDuster

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top