Subscribe for Weekly Tutorials
BONUS: subscribe now to download our Top Tutorials Ebook!
UserForm Controls Events
Explanation of Events for Controls in UserForms in Excel. This includes what they are, how to use them, and the most common Event used for Controls in UserForms.
This tutorial is specific to the events for controls and is not for the UserForm events. This tutorial covers many of the same concepts as the UserForm Events tutorial.
Sections:
What is a UserForm Control Event?
What is a UserForm Control Event?
Events are actions that happen within Excel, in this case, with a control in a form in Excel.
Events allow you to write code that will execute when a specific action occurs. These actions can be caused by a user or other macros/VBA within Excel and include things such as when a button is clicked; when the user clicks a button, the code that you wrote for the event will cause something to happen.
Note: events are not just for controls and UserForms; they also work with worksheets and workbooks and each one of these elements has events that are specific to them.
The code for events in forms goes into the code window specific to that form, as explained in the next section.
How to Use an Event
Events are placed wtihin the code window that is specific to the UserForm in which they are contained.
Go to the VBA window (Alt+F11) > look to the Project window on the left side (Ctrl+R) > look in the Forms folder > right-click the desired form > click View Code.
Once you do this, you should see the code window for that UserForm. This is where all the code that will control the form goes.
Look to the two drop-down menus at the top of this window:
The drop-down on the left contains the names of the controls for the form, including the form itself.
The menu on the right contains all of the events that are specific to the control that you chose from the left drop-down menu.
From the left menu, choose the name of the control that you want to work with. The option UserForm references the form itself and not a specific control.
Once you do this, a section of code will appear for whatever event was already selected in the right drop-down menu; don't worry about this if it is the wrong event, simply delete the section of code that appeared when that happened and then go to the right drop-down menu and select the correct event.
The most common event that is used with a control is the Click event. The code for this event is run when the user clicks the control, for instance, when a user clicks a button on the form.
Once you select an event, a code section will appear like this:
All code that you want to run when this event occurs should be placed within this section of code.
Format of the Event
Each event code section will look like this:
Private Sub NameOfControl_EventName()
'Code goes here
End Sub
In the above example, CommandButton1 is the name of the command button control that we are writing code for and Click is the event that triggers the code to run for the control.
Add Code
In this example, the command button to which we are adding code is the button that should close the form when it is clicked. To do this, we add a simple line of code within the code section.
Private Sub CommandButton1_Click()
Unload Me
End Sub
Unload Me is the code that causes the form to close. Since we put this into the Click event for a command button, this code will run whenever that button is clicked by the user.
This is how we get buttons to do things in UserForms.
Quickly Add Code to the Click Event for a Button
When you view the UserForm in the VBA window, you can quickly add a Click event to a button by double-clicking that button. This automatically opens the code window for the form and takes you to the click event for that button.
Most Common Control Event
The Click event is the most commonly used event for a control. This is most often used with buttons on a form so that something happens when the user clicks a button. This could be anything from closing the form to taking data from the form and storing it in a worksheet.
Notes
The different types of controls, button, listbox, combobox, etc. have slightly different events available to them since each control is unique and serves a different purpose. As such, the possible events for each control will not be listed in this tutorial. However, most of the events for a control are self-explanatory.
Make sure to download the sample Excel file to see some control events for UserForms.