This article will demonstrate the role of the userform_initialize event in VBA.
The UserForm_Initialize event runs when the UserForm is first opened.
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”.
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