When building UserForms in VBA, you often need dynamic controls that respond to situations, such as: “I want to enable a text box only when a checkbox is checked,” or “I want to switch which items are displayed based on an option button selection.”
By manipulating the .Enabled (Enable/Disable) and .Visible (Show/Hide) properties of controls using VBA code, you can easily create these kinds of interactive forms.
In this article, I will explain practical techniques to switch the availability and visibility of other controls in real-time based on user actions.
VBA Sample Code: Switching Control States
Here, we will create a process for a common “Shipping Address” form. When a “Same as Billing Address” checkbox is checked, the shipping address input fields will be disabled (grayed out), and an “Input Not Required” label will appear.
UserForm Code
Write this code inside the _Click() event of a checkbox named SameAsBillingAddress_CheckBox.
' Process when the "SameAsBillingAddress" checkbox is clicked
Private Sub SameAsBillingAddress_CheckBox_Click()
' Store whether the checkbox is On (True) or Off (False) in a variable
Dim isChecked As Boolean
isChecked = Me.SameAsBillingAddress_CheckBox.Value
' --- Switch availability using the Enabled property ---
' If isChecked is True, Enabled becomes False (Disabled)
' If isChecked is False, Enabled becomes True (Enabled)
Me.ShippingAddress_TextBox.Enabled = Not isChecked
Me.ShippingCity_TextBox.Enabled = Not isChecked
Me.ShippingZipCode_TextBox.Enabled = Not isChecked
' --- Switch visibility using the Visible property ---
' If isChecked is True, Visible becomes True (Shown)
' If isChecked is False, Visible becomes False (Hidden)
Me.InputNotRequired_Label.Visible = isChecked
End Sub
Preparation Before Execution
- Create a UserForm.
- Place 1 CheckBox, 3 TextBoxes, and 1 Label on the form.
- Change their
(Name)properties to match the code:SameAsBillingAddress_CheckBoxShippingAddress_TextBoxShippingCity_TextBoxShippingZipCode_TextBoxInputNotRequired_Label
- When you display the form and click the checkbox, the state of the text boxes and the label will switch.
Explanation of the Code
.Enabled = Not isChecked
The .Enabled property controls whether a user can interact with the control.
- True: The control is active (Enabled).
- False: The control is inactive (Disabled/Grayed out).
The Not operator inverts a Boolean value (True/False).
- Checkbox is On (
isChecked = True):Not isCheckedbecomesFalse, so the text boxes become Disabled. - Checkbox is Off (
isChecked = False):Not isCheckedbecomesTrue, so the text boxes become Enabled.
Using the Not operator allows you to smartly link the checkbox state to the enabled/disabled state of other controls.
.Visible = isChecked
The .Visible property controls whether the control is displayed on the screen.
- True: The control is shown.
- False: The control is hidden.
Here, we do not use Not but assign the checkbox state directly.
- Checkbox is On (
isChecked = True): The label’sVisibleproperty becomesTrue, so it is Shown. - Checkbox is Off (
isChecked = False): The label’sVisibleproperty becomesFalse, so it is Hidden.
Summary
In this article, I explained how to switch the Enabled/Disabled and Visible/Hidden states of UserForm controls using VBA.
- Use the
.Enabledproperty (True/False) to switch operability. - Use the
.Visibleproperty (True/False) to switch visibility. - Use the
Notoperator to invert Boolean values for easy toggle logic.
By utilizing these properties within event procedures (like _Click()), you can create intuitive and user-friendly forms that respond to user actions in real-time.
