Vba form events in Excel
The form events are the one associated with the forms.
The form is the base container that holds all other controls within it.
Same as other controls, that have some events associated with it, The form too has someimportant events.
Some of the most commonly used events are discussed as below:
1.Initialize Event
This event is trigerred for the first time when the form is opened or initialized.
This event is helpful in cases where a combobox or listbox value has to be filled.
The following code shows the use of the initialize event.
- Private Sub UserForm_Initialize()
- ComboBox1.AddItem "India"
- ComboBox1.AddItem "USA"
- ComboBox1.AddItem "Canada"
- ComboBox1.AddItem "France"
- End Sub
The above code populates a combobox with values while the form is loaded for the first time.
The screenshot of the code and the output is as shown below:
From the above exampe, it is seen that the combobox values are populated with the form load.
This makes easier for the enduser to choose the values instantly.
2.Activate event
This event is triggered whenever the form becomes active.
This event is best used while working with multiple forms in an application.
The following snippet demonstrates this event;
- Private Sub UserForm_Activate()
- MsgBox "Form Activated"
- End Sub
The screenshot of the code and the output are as shown below: