How to Trigger the Escape Key from a VBA Cancel Button
Question details
The user wants to configure a VBA Cancel button to trigger the Escape (ESC) key or handle cancellation logic to stop macro execution.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Setting up a VBA UserForm or a worksheet form control where a specific button needs to interrupt a process or close a dialog similar to pressing the Escape key.
- Observed behavior
- The user needs to know the correct VBA syntax for sending the Escape key command and the structural differences between assigning macros to UserForm buttons versus worksheet form controls.
Ensure the Developer tab is enabled in your spreadsheet application so you can access form controls and the VBA Editor.
Configure Cancellation Logic in a UserForm Button
Use an event procedure directly within the UserForm module to handle cancellation explicitly, which is the most reliable method for UserForms.
UserForm buttons rely on event procedures rather than standard assigned macros. Using explicit cancellation commands (like closing the form) is generally safer and more predictable than simulating keystrokes.
Press Alt+F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer, double-click your UserForm to view it, then double-click the Cancel button to automatically generate its click event.
Inside the generated Private Sub btnCancel_Click() procedure, insert your cancellation logic (for example, typing 'Unload Me' to close the form).

Simulate the Escape Key Using Application.SendKeys
Use the SendKeys method to programmatically send an Escape keystroke to the active window.
Assign a Cancellation Macro to a Worksheet Form Control
If you are using a button placed directly on the worksheet instead of a UserForm, you must assign a standard macro module to it.
Write and Execute VBA Macros Effortlessly with WPS Office
WPS Spreadsheet provides comprehensive support for VBA and macros, allowing you to create UserForms, insert form controls, and automate your workflows exactly as you would in Microsoft Excel.
- 1. Download and Install: Download WPS Office Free and launch the WPS Spreadsheet application.
- 2. Enable the Developer Tab: Navigate to the ribbon to access the Developer tab where you can manage macros, form controls, and launch the VBA editor.
- 3. Create and Run Macros: Use Alt+F11 to open the VBA environment, write your procedures, and bind them to your spreadsheet buttons seamlessly.

Frequently Asked Questions
What is the difference between worksheet form controls and UserForm buttons?
Worksheet form controls are UI elements inserted directly onto the spreadsheet grid and are triggered by assigning standard macros to them. UserForm buttons are embedded inside custom dialog boxes (UserForms) and operate using event procedures, such as 'Private Sub btnCancel_Click()', stored within the form's specific code module.
Why is explicit cancellation logic preferred over Application.SendKeys?
The Application.SendKeys method sends simulated keystrokes to whatever window is currently active. If a user clicks away to another application or system notification pops up, the '{ESC}' key might be sent to the wrong program, causing unexpected behavior. Explicit logic, like modifying a boolean variable or unloading a form, runs safely entirely within the macro.
How do I show the Developer tab to access form controls?
To access the Developer tab in your spreadsheet, go to File > Options > Customize Ribbon. Check the box next to 'Developer' in the main tabs list and click OK. This will make the Developer tab visible, giving you access to the Form Controls menu and the VBA editor.




