userform_initialize VBA
Written by
Reviewed by
This article will demonstrate the role of the userform_initialize event in VBA.
UserForm_Initialize Event
The UserForm_Initialize event runs when the UserForm is first opened.
Often it will contain code to populate controls such as ComboBoxes, or adjust which controls are visible.
Customize UserFormControls
This example will populate a ComboBox in a UserForm:
Private Sub UserForm_Initialize()
Me.Caption = "Enter Order Details"
Me.BackColor = vbYellow
Me.cmdCancel.Enabled = False
'add items to the combo box
Me.cboRep.AddItem "Bob Smith"
Me.cboRep.AddItem "Jane Doe"
Me.cboRep.AddItem "Jim Jones"
Me.cboRep.AddItem "Brad Hilbert"
Me.cboRep.AddItem "Sandy Sinatra"
End Sub
This code would result in the following items being added to the drop down list “Rep”.
UserForm Properties
You can also use the Initialize event to adjust properties of the UserForm itself.
This UserForm_Initialize event will do the following:
- Set the UserForm Caption
- Set the UserForm background color
- Disable the Cancel button.
Private Sub UserForm_Initialize()
Me.Caption = "Enter Order Details"
Me.BackColor = vbYellow
Me.cmdCancel.Enabled = False
End Sub