UserForm Controls Events

Add to Favorites
Author: | Edits: don

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?

How to Use an Event

Most Common Control Event

Notes

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.

e8b07d0918ca6ae091caf16e6f575838.jpg

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.

2249ca3516cdba06f33967fac40a9cc6.png

Look to the two drop-down menus at the top of this window:

27c0dc67339f9604e0e29ca7d014d2ad.png

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.

066d5898dc487ee828a23ea854ee7f96.png

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:

62106dfd1d265b76876d0e56dd5160ad.pngAll 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.

Question? Ask it in our Excel Forum


Downloadable Files: Excel File

Similar Content on TeachExcel
Put Data into a UserForm
Tutorial: How to take data from Excel and put it into a UserForm. This is useful when you use a form...
UserForm Controls
Tutorial: This tutorial gives you an overview of what a UserForm Control is and how to add them to y...
Create UserForm Control Templates
Tutorial: How to create a UserForm Controls template that will allow you to quickly add pre-formatte...
UserForm Events
Tutorial: Explanation of UserForm Events in Excel. This includes what they are, how to use them, and...
Disable/Enable Buttons in UserForms
Tutorial: How to have a button in a UserForm disabled until the desired event occurs. For instance, ...
UserForm Properties List and Explanation
Tutorial: This is a listing of all properties for the UserForm in Excel. Each property includes an e...
Tutorial Details
Downloadable Files: Excel File
Similar Content
Put Data into a UserForm
Tutorial: How to take data from Excel and put it into a UserForm. This is useful when you use a form...
UserForm Controls
Tutorial: This tutorial gives you an overview of what a UserForm Control is and how to add them to y...
Create UserForm Control Templates
Tutorial: How to create a UserForm Controls template that will allow you to quickly add pre-formatte...
Excel Forum