Search code examples
excelvbacheckboxinitializationuserform

Excel userform not opening in unchecked form


I need a userform to load the the checkbox values in their unchecked form. They open with the boxes unchecked but they don't seem to be initializing that way. Here is the checkbox code and the open button code I have.

Private Sub cbxAdditional_Click()

    If Me.cbxAdditional.Value Then
        Me.Width = 524
        Me.Height = 238.5
        lstDatabase.Width = 480
        txbxName.Visible = True
        txbxComments.Visible = True
    Else
        Me.Width = 279
        Me.Height = 238.5
        lstDatabase.Width = 240
        txbxName.Visible = False
        txbxComments.Visible = False
    End If
End Sub

--open code

Sub openform()

Test.Show
cbxAdditional = False

End Sub

I clearly am writing this wrong, when I opne the user form it starts in the IF form, how do I get it to open in the unchecked form?


Solution

  • Here's a very simple userform:

    enter image description here

    When you enter a userform, the UserForm_Initialize sub is executed. This sub is NOT automatically created in the code with the userform, but you can create it yourself. Any initialization or set up code should be performed here.

    In my example below, I'm showing that the resetting of the checkboxes is a separate sub. I do this as a habit, just in case I have to add a reset button or something.

    Option Explicit
    
    Private Sub ClearCheckboxes()
        Me.CheckBox1.Value = False
    End Sub
    
    Private Sub ResetButton_Click()
        ClearCheckboxes
    End Sub
    
    Private Sub UserForm_Initialize()
        ClearCheckboxes
    End Sub