Open In App

UserForms in Excel Macros

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:

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 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 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 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.

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

Article Tags :