Escape keypress action

Andrew_LEJOG

New Member
Joined
Jun 18, 2008
Messages
2
I have a potentially time consuming loop in a macro, within which I would like to give the user the option of pressing the ESCAPE <ESCAPE><ESCAPE>button which would then run another section of code (see the code below).

I would have thought that the Application.OnKey method would achieve this but nothing seems to happen when I press <ESCAPE>.

If anyone can suggest a solution, I would be very grateful.

Sub t()
MsgBox "Escape pressed"
End Sub

Sub Macro1()
For x = 1 To 100000
Application.OnKey "{ESC}", "t"
Next x
End Sub

Many thanks.

Andrew.
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Many thanks Andrew Poulsom for your reply. It put me along the right lines.
In fact I'll probably use some code along the following lines:

Sub test()
On Error GoTo handleCancel
Application.EnableCancelKey = xlErrorHandler
MsgBox "This may take a long time: press ESC to cancel"
For x = 1 To 500000000 ' Do something 1,000,000 times (long!)
' do something here
Next x
MsgBox "Finished"
Exit Sub
handleCancel:
If Err = 18 Then
MsgBox "You cancelled"
End If
End Sub


Many thanks again.

Andrew_LEJOG
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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
Back
Top