VBA Can i make a Emergency Stop button for code?

NessPJ

Active Member
Joined
May 10, 2011
Messages
405
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,815
Office Version
  1. 2010
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
14,815
Office Version
  1. 2010
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:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,530
Messages
5,837,907
Members
430,520
Latest member
VenkateshRajaganesan

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