logo
search
Office Settings & Configuration

How to Trigger the Escape Key from a VBA Cancel Button

Bushra ParveenBushra Parveen Oct 12, 2026 868 views

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.

How to Trigger the Escape Key from a VBA Cancel Button
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.
Before you start

Ensure the Developer tab is enabled in your spreadsheet application so you can access form controls and the VBA Editor.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt+F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Access the Button Event

In the Project Explorer, double-click your UserForm to view it, then double-click the Cancel button to automatically generate its click event.

3
Add Cancellation Code

Inside the generated Private Sub btnCancel_Click() procedure, insert your cancellation logic (for example, typing 'Unload Me' to close the form).

Configure Cancellation Logic in a UserForm Button

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. 1. Download and Install: Download WPS Office Free and launch the WPS Spreadsheet application.
  2. 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. 3. Create and Run Macros: Use Alt+F11 to open the VBA environment, write your procedures, and bind them to your spreadsheet buttons seamlessly.
Seamlessly compatible with Microsoft Excel VBA scripts and xlsm formatsBuilt-in Developer tools for UserForms and Form ControlsLightweight architecture ensures macros run quickly and smoothlyFree alternative with a highly familiar user interface
microsoft office alternative - wps office

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.