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:
- Place a CheckBox and a TextBox on the form.
- Change the CheckBox
(Name)toEnableDetails_CheckBoxandCaptionto “Enter Details”. - Change the TextBox
(Name)toDetails_TextBoxand set itsEnabledproperty toFalsein the Properties window (initial state). - 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 isTrueif On, andFalseif Off.Me.Details_TextBox.Enabled = ...: Directly assigns the retrieved checkbox state to the text box’sEnabledproperty.
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’sValueproperty 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
.Valueproperty (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.
