[Excel VBA] How to Enable/Disable and Show/Hide UserForm Controls

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

  1. Create a UserForm.
  2. Place 1 CheckBox, 3 TextBoxes, and 1 Label on the form.
  3. Change their (Name) properties to match the code:
    • SameAsBillingAddress_CheckBox
    • ShippingAddress_TextBox
    • ShippingCity_TextBox
    • ShippingZipCode_TextBox
    • InputNotRequired_Label
  4. 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 isChecked becomes False, so the text boxes become Disabled.
  • Checkbox is Off (isChecked = False): Not isChecked becomes True, 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’s Visible property becomes True, so it is Shown.
  • Checkbox is Off (isChecked = False): The label’s Visible property becomes False, 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 .Enabled property (True/False) to switch operability.
  • Use the .Visible property (True/False) to switch visibility.
  • Use the Not operator 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.

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

この記事を書いた人

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

目次