UserForms in Excel Macros
Last Updated :
01 Jun, 2021
A UserFrom refers to a form or a dialog box on the screen that enables a user to input data into Excel's user interface. it can be created using the Visual Basic Editor and is used to control data entry and avoid errors.
1. Creating a UserForm:
To create a userform head on to the Developer tab-> Visual Basic->Insert->UserForm.
You can also go to the Project window on the left side and right-click the workbook you want to use and select Insert->UserForm. This opens up a blank userform (UserForm1) and a toolbox(if the toolbox doesn't pop up head on to View>toolbox).
2. Naming the UserForm:
By default, excel opens up an userform with the name UserForm1 in order to change that name:
- Head to the Properties window(view>Properties window), This opens a pane on the left.
- Click on the 'Name' section and type a name of your choice. (don't add spaces)
- The name won't be reflected on the userform when you do so because that's a different property of the form.
- Head to the 'Caption' property and type in a name, and it will be reflected on your UserForm.
Now let's play with some controls and learn how to add them to our UserForm.
3. Adding a Textbox to the UserForm:
A Textbox is used to enable users to input information into the form.
- To add a TextBox head onto the ToolBox. Click on( View->Toolbox) if it isn't visible by default.
- Select the TextBox button
- Click on the userform to place it or drag it to a position of your choice.
- You can resize the Textbox to the dimensions of your liking
To rename the Textbox With the new textbox selected, double-click on the 'Name' property in the Properties window and rename it(its been renamed to 'TextBox' here).

4. Adding a Label to the UserForm:
A Label can be used to describe what type of data is being entered into the textboxes, a message, or instructions.
- To add a Label head onto the ToolBox.
- Select the 'Label 'button
- Click on the userform to place it or drag it to a position of your choice.
- You can resize it to the dimensions of your liking or leave it in standard size.
To rename the Label With the new Label selected, double-click on the 'Name' property in the Properties window and rename it. Also, change the 'Caption' property accordingly. its been renamed to 'LblSelectSheet' and captioned to 'Please select a worksheet'.

5. Adding Buttons to the Userform:
To make the form interactive you can place buttons to perform certain actions.
- To add a Button head onto the ToolBox.
- Select the 'CommandButton' button
- Click on the userform to place it or drag it to a position of your choice.
- You can resize it to the dimensions of your liking or reposition the buttons by dragging them to a new location on the form.
To rename the button With the new button selected, double-click on the 'Name' property in the Properties window and rename it. Then change the 'Caption' property to name the button relevantly close to the action it will be performing. For example the AddSheet and CloseFom button in the image below. In the below images 2 buttons have been added and renamed to 'CmdAddSheet' and 'CmdCloseFrm' and captioned to 'Add Sheet' and 'Close Form'.


6. Adding code to the buttons:
To ensure the buttons are performing the actions we need to code inside VBA and to add code for specific controls double-click on the controls in the form, and it directs you to the specific code segment. After adding the necessary code for each of the controls click on the run button to display the userform.
- Select the button for example Add sheet button in this example
- On the Menu bar, choose View >Code.
- This creates a procedure, where you can add your code.
- Enter the following code to add another sheet to your workbook and name it according to your choice.
Private Sub CmdAddSheet_Click()
Worksheets.Add before:=Worksheets(1)
ActiveSheet.Name = InputBox("please enter the name for the worksheet")
End Sub
7. Adding code to the close button :
Add the following code to close the form by clicking on the 'close from' button and redirect to the VBE.
Private Sub CmdCloseFrm_Click()
Unload Me
End Sub
8. Testing the userform :
To test the UserForm simply go to the menu bar and hit the Run button. It runs userform and displays it on the screen. For example, a userform that selects a sheet from the combobox and directs you to it, with 2 buttons Add Sheet and Close form button to add an extra sheet in the workbook and the close the userform is displayed.

Complete code to run the above userform inclusive of a combobox with the list of worksheets currently present in the workbook and functionality of buttons is:
Private Sub CmdAddSheet_Click()
Worksheets.Add before:=Worksheets(1)
ActiveSheet.Name = InputBox("please enter the name for the worksheet")
End Sub
Private Sub CmdCloseFrm_Click()
Unload Me
End Sub
Private Sub combobox_Change()
Worksheets(Me.combobox.Value).Select
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
i = 1
Do While i <= Worksheets.Count
Me.combobox.AddItem Worksheets(i).Name
i = i + 1
Loop
End Sub
To automate the userform experience or to display it as soon as you open the workbook the following needs to be done:
Making use of the workbook event
- Head on to the Developer tab>Visual Basic.
- Double click "This workbook'' from the project window.
- Change the drop-down on the top left from General to workbook.
- Add this line of code to the procedure "formname.show"(in the below example frmFinalReport.show).
- Save and run.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read