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.

  1. Private Sub UserForm_Initialize()
  2. ComboBox1.AddItem "India"
  3. ComboBox1.AddItem "USA"
  4. ComboBox1.AddItem "Canada"
  5. ComboBox1.AddItem "France"
  6. 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:

excel vba form events

excel vba form events

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;

  1. Private Sub UserForm_Activate()
  2. MsgBox "Form Activated"
  3. End Sub

The screenshot of the code and the output are as shown below:

excel vba form events

excel vba form events

 

You can find similar Excel Questions and Answer hereunder

1) How to control forms in Excel VBA

2) How can I use SUMPRODUCT to summarize my raw data?

3) I frequently use a Macro - is there a way to quickly access the Macro in the Excel Ribbon?

4) How to create forms in Excel VBA

5) I have data arranged in rows of information. Is there any way I can enter new data through a form instead of entering it directly in SpreadSheet?

6) I have a WorkBook that loads a form automatically when it is opened. In Excel, how can I suppress the form from loading on file open when required?

7) I have a cell with nested formulas that returns an error. In Excel, how can I step through the formulas to help with debugging?

8) How can I quickly remove all blank cells in a data range?

9) Keyboard shortcut for save as in Excel

10) Here an explanation about how to control the folder dialog that will let you access to your files using VBA

 

Here the previous and next chapter