[VBA] How to Set Default (Enter) and Cancel (Esc) Buttons in UserForms

目次

Introduction

In standard Windows dialog boxes, pressing the Enter key acts as clicking “OK,” and pressing the Esc key acts as clicking “Cancel.” This behavior is intuitive for users and significantly improves the efficiency of keyboard operations.

In VBA UserForms, you can easily implement this convenient feature by setting the .Default and .Cancel properties of your command buttons.

In this article, I will explain how to map specific buttons on a UserForm to the Enter and Esc keys.

VBA Sample Code: Setting Default and Cancel Buttons

Usually, you set the .Default and .Cancel properties within the UserForm_Initialize event, which runs just before the form is displayed.

In this example, we will set OKButton as the default button and CancelButton as the cancel button on a form named ConfirmForm.

UserForm Code

' Event executed when the form is initialized
Private Sub UserForm_Initialize()
    
    ' --- Set OKButton as the "Default Button" ---
    ' By doing this, pressing the Enter key on the form triggers this button.
    Me.OKButton.Default = True
    
    ' --- Set CancelButton as the "Cancel Button" ---
    ' By doing this, pressing the Esc key on the form triggers this button.
    Me.CancelButton.Cancel = True
    
End Sub

' Process when the "OK" button is clicked
Private Sub OKButton_Click()
    MsgBox "OK was selected."
    Unload Me
End Sub

' Process when the "Cancel" button is clicked
Private Sub CancelButton_Click()
    MsgBox "Cancel was selected."
    Unload Me
End Sub

Preparation Before Running

  1. Create a UserForm named ConfirmForm.
  2. Place two CommandButtons on the form.
  3. Change their (Name) properties to OKButton and CancelButton.
  4. Change their Caption properties to “OK” and “Cancel”.
  5. Paste the code above into the form’s code module.
  6. When you display the form and press Enter or Esc, you will see the corresponding message box.

Code Explanation

Me.OKButton.Default = True

Setting a command button’s .Default property to True makes it the Default Button.

  • When the form is active and the Enter key is pressed, the _Click() event for this button is executed.
  • This is typically set for buttons like “OK” or “Run” that are most likely to be used.
  • Only one button per form can have .Default = True. If you set it to True for another button, the previous one automatically becomes False.

Me.CancelButton.Cancel = True

Setting a command button’s .Cancel property to True makes it the Cancel Button.

  • When the form is active and the Esc key is pressed, the _Click() event for this button is executed.
  • This is typically set for buttons like “Cancel” or “Close” that interrupt the process.
  • Similar to the .Default property, only one button per form can have .Cancel = True.

Summary

In this article, I explained the .Default and .Cancel properties to improve UserForm usability.

  • .Default = True: Sets the button that responds to the Enter key.
  • .Cancel = True: Sets the button that responds to the Esc key.
  • It is common to apply these settings in the UserForm_Initialize event.

By adding these simple settings, your UserForm will behave like a professional dialog box, making it much easier for keyboard users to use. Please try incorporating this into your form development.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次