Events

Running the VBA program is most often initiated by an event. Events can be on the document-level (eg Open, Activate), worksheet (eg Calculate, Change), charts, applications, forms … The event that starts the subroutine is mainly a click on a button or object, object focus or change the content of the object. Some events are executed automatically after opening an existing or creating a new document …

When events automatically trigger an action, the VBA code is written within the procedures- subroutines named after those events. For example, if we want the program to be executed automatically after the opening of the worksheet (document) it will initiate the Workbook_Open event.

How does this work? First open the new Excel document, and then from the Developer ribbon add the ActiveX control that we will use to select the country name (ComboBox1). Open VBA Editor (ALT + F11) and in the Project Explorer window, select ThisWorkbook. Here we create subroutines that are executed in the context of a workbook. So if we want to execute a program by which we initiate ComboBox1 control, by adding country names and selecting the default items in the list, we will write it in within a subroutine called Workbook_Open.

Private Sub Workbook_Open()

Application.Sheets(1).ComboBox1.AddItem “ALL”
Application.Sheets(1).ComboBox1.AddItem “AUSTRIA”
Application.Sheets(1).ComboBox1.AddItem “CROATIA”
Application.Sheets(1).ComboBox1.AddItem “GREECE”
Application.Sheets(1).ComboBox1.AddItem “HUNGARY”
Application.Sheets(1).ComboBox1.AddItem “ITALY”
Application.Sheets(1).ComboBox1.AddItem “MONTENEGRO”
Application.Sheets(1).ComboBox1.AddItem “SERBIA”

Application.Sheets(1).ComboBox1.ListIndex = 0

End Sub

If you save an Excel document as the one that supports macros, after restarting, you will see that the combo list is filled with values, with the default value of “ALL”.