[VBA] How to Execute Code in Real-Time When a CheckBox State Changes

目次

Introduction

In VBA UserForms, there are frequent scenarios where specific processing needs to be executed the moment a checkbox is toggled on or off. A common example is interactive behavior, such as “enabling a text box for detailed input only when the ‘Other’ checkbox is checked.”

Such real-time response can be implemented using the CheckBox’s _Click() event (or _Change() event).

This article explains how to create a practical macro that changes the state of another control the instant a user clicks a checkbox.

VBA Sample Code: Executing Processing on State Change

This macro toggles the enabled/disabled state of a text box named Details_TextBox when a checkbox named EnableDetails_CheckBox is clicked.

UserForm Code

Write this code directly into the UserForm’s code module.

Preparation:

  1. Place a CheckBox and a TextBox on the form.
  2. Change the CheckBox (Name) to EnableDetails_CheckBox and Caption to “Enter Details”.
  3. Change the TextBox (Name) to Details_TextBox and set its Enabled property to False in the Properties window (initial state).
  4. Double-click the placed CheckBox to automatically generate Private Sub EnableDetails_CheckBox_Click() in the VBE, then write the processing inside.
' Event executed when the "Enter Details" checkbox (EnableDetails_CheckBox) is clicked
Private Sub EnableDetails_CheckBox_Click()

    ' Assign the current state of the checkbox (True/False)
    ' directly to the Enabled property of the text box
    Me.Details_TextBox.Enabled = Me.EnableDetails_CheckBox.Value

End Sub

Code Explanation

Private Sub EnableDetails_CheckBox_Click()

This is a specific event procedure. The code inside runs automatically the moment the control named EnableDetails_CheckBox is clicked. This event occurs every time the user clicks, regardless of whether the state turns On or Off.

Me.Details_TextBox.Enabled = Me.EnableDetails_CheckBox.Value

This single line achieves real-time synchronization.

  • Me.EnableDetails_CheckBox.Value: Retrieves the current state of the clicked checkbox. It is True if On, and False if Off.
  • Me.Details_TextBox.Enabled = ...: Directly assigns the retrieved checkbox state to the text box’s Enabled property.

This results in an intuitive and smart linkage:

  • Checkbox ON (Value = True) → TextBox Enabled (Enabled = True)
  • Checkbox OFF (Value = False) → TextBox Disabled (Enabled = False)

Difference from _Change() Event

CheckBoxes have a _Change() event in addition to _Click(). Both occur when the check state changes, so they can often be used similarly. However, there is a distinction:

  • _Click(): Linked to the action of the user clicking with the mouse.
  • _Change(): Occurs even if the control’s Value property is changed by code, in addition to user interaction.

If the goal is to react to direct user operation, using _Click() is generally preferred and easier to understand.

Summary

This article explained how to execute processing in real-time when a checkbox state changes.

  • Use the control’s _Click() event procedure to react immediately to user click operations.
  • The current state of the checkbox can be retrieved via the .Value property (True/False).

Using this technique allows for the dynamic alteration of the form’s appearance and behavior according to user selection, enabling the creation of more user-friendly and intuitive tools.

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

この記事を書いた人

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

目次