[ESC] and [CTRL] + [BREAK] don't seem to work

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
I'm having a rather serious problem.

My macro uses a lot of SendKeys commands to operate another program. It's also full of make-shift delays between the SendKeys commands, using a function that runs a pointless loop.

Sometimes I need to suddenly Exit the macro; to stop further SendKeys commands from running. But despite what I've read, using either Esc or Ctrl+Break just doesn't seem to work!

The only way I seem to be able to exit it is with the Task manager.

Any ideas as to why this is so?
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
If you are working with another application the CTRL+BREAK might be getting 'sent' to that rather than Excel.

That's probably what's happening, though I have seen VBA not respond to to things like that before.
 

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
Hmm I think you're right. That's a bit of a pain.

Is there any way I could code a dedicated kill-switch into my macro, then? If I could do that, that would be perfect.
 

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
I don't like to bump threads but they get buried easily and I'm sure there's a solution to this one! Thanks.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
Perhaps you could start by getting rid of, or altering the 'pointless' loop.

Other than that what would be the trigger for your killswitch?

I suppose if you ran the code from a form then you could add a button and have a real switch.

Using DoEvents would probably help with monitoring for it being clicked while the rest of the code is still running.
 

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
The pointless loop is there to cause a delay between SendKeys commands... I've had a good look around the internet for code that would create delays and the best I can come up with is this function:

Code:
Function Delay(seconds As Integer)

    Dim StartTime As Date
    StartTime = Now
    
    Do While DateDiff("s", StartTime, Now) < seconds
    Loop
    
End Function

Ideally I'd like the killswitch trigger to just be a key on the keyboard (that works whether or not Excel is in focus). But it's possible there's no way to do that in VBA.

The macro is literally just a list of SendKeys commands... it starts with an Alt+Tab to switch to another program and the rest interact with that program (navigating menus, selecting options etc). Some commands also send strings from the spreadsheet to the program (i.e. SendKeys mystring, True). It loops a bit to do the same thing repeatedly, and then ends.

I think what I may have to do instead, is use AutoIt, and program in a few Alt+Tabs and Ctrl+C/V to copy data over from the spreadsheet into the program instead. It's a bit messier but I know that AutoIt supports a killswitch.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,694
Messages
5,597,571
Members
414,156
Latest member
WDMix

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