userform_initialize VBA

Written by

Mel Jenkins

Reviewed by

Steve Rynearson

Last updated on May 11, 2022

This article will demonstrate the role of the userform_initialize event in VBA.

 

vba initialize event

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”.

vba initialize dropdown

 

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

vba initialize macro

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples