UserForms in Excel Macros - GeeksforGeeks

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.
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).
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.
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).
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’.
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’.
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
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
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.
Picked
Excel
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Find the Last Used Row and Column in Excel VBA?
Introduction to Excel Spreadsheet
How to Use Solver in Excel?
How to Show Percentages in Stacked Column Chart in Excel?
Using CHOOSE Function along with VLOOKUP in Excel
How to Remove Duplicates From Array Using VBA in Excel?
How to Get Length of Array in Excel VBA?
Macros in Excel
How to Check if the Number is Prime Number in Excel?
How to Extract the Last Word From a Cell in Excel?