Excel VBA Userform
Userform in VBA are customized user-defined forms which are made to take input from a user in the format of a form, it has different sets of controls to add such as text boxes, checkboxes labels, etc to guide a user to input a value and it stores the value in the worksheet, every part of user form has a unique code with it.
Userform is an object within the Excel interface, and inside this user form, we can create other useful custom dialogue boxes to get the data from the user. When you are working with a macro file that is created by your senior or may be downloaded from the internet, you must have seen a userform like this.
In this article, we will show you how to create a similar userform to store the data from the user.
How to Create Userform?
- Like how you insert a new module similarly, you need to click on the INSERT button in Visual Basic Editor to insert Userform.
- As soon as you click on this, the user form will also be inserted.
Before I tell you how to program this, let me show you how to format this userform.
By selecting the userform, press the F4 key, it will show you the properties window.
Using this properties window, we can format this userform. We can name it. We can change the color, border style, etc.…
Like this, try other properties to get the sight of the userform.
Now for this userform, insert Toolbox.
Now we will see a toolbox like this.
At this moment, the userform is just inserted, not programmed. To know how it works, just click on the run button. We will see the form on the excelForm On The ExcelThe form in Excel simplifies data entry by allowing us to view, add, edit, and delete one record in a horizontal orientation. It is a hidden feature that isn't accessible through the ribbon tools. This functionality must be added using the quick access toolbar option under the File tab. worksheet.
Now using the ToolBox draw label.
Enter the Text as Employee Name inside the label.
For this label, so we can format by using properties. Now we have entered the text as “Employee Name:” now we can see this in the properties window under Captions.
Insert one more label. To insert one more label, either you can click on a toolbox, or you can drag the current label by holding the Ctrl key. You will have a replica of the current label.
Now we will have the same label.
Change the name to Employee ID.
Now similarly insert one more label and name it as “Department.”
Now from the toolbox, insert a text box.
Name this text box as EmpName in the properties window.
Like this, insert two more text boxes in from of Employee ID & Department, respectively. Name those text boxes as per their heading.
Similarly, do it for the Department.
Now from the toolbox, insert Command Button.
Change the Name of the Command Button to “SubmitButton” and change the caption to “Submit.”
Insert one more button and call it “Cancel.”
Now just to see run, press the run button or use the F5 key and see how your userform looks like in Excel.
Now it is coming to the shape.
Now the user will enter data in this, so we need to program this to store the data entered by the user on this form.
Double click on the SUBMIT button, it will take you to the macro window with an auto-created macro like the below.
It says SubmitButton click, remember we had given a name for the SUBMIT button as SubmitButton.
So, whenever we want to call this button, we can call this by this name (submit button). Inside this macro, copy and paste the below code.
Private Sub SubmitButton_Click() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row + 1 Cells(LR, 1).Value = EmpName.Value Cells(LR, 2).Value = EmpID.Value Cells(LR, 3).Value = Dept.Value EmpName.Value = "" EmpID.Value = "" Dept.Value = "" End Sub
- EmpName.Value here EmpName is the text box name we had given while creating the employee name text box.
- EmpID.Value here EmpID is the text box name of the Employee ID text box.
- Dept.Value this is the department text box name.
So, on the click on the submit button, it will store the values in the mentioned cells.
Now double click on the Cancel button. This will also show you the auto macro name like this.
Copy the below code and paste.
Private Sub CancelButton_Click() MyUserForm.Hide End Sub
MyUserForm is the name we had given to the userform. MyUserForm.Hide means on the click on the CANCEL button it will hide the userform.
Ok, now create a template like this in the worksheet.
Delete all the other sheets in the workbook except this template sheet.
Now go to Visual Basic Editor.
And run the macro using the F5 key or manually, we will see user form in front of us.
Enter the employee name, employee id, and Department name.
Now, if you click on the SUBMIT button, it will store the values on to the template we have created.
Like this, you can keep entering the names. The userform will keep storing the values entered by the user in the specified cells.
So by using USER FORM, we can actually create wonderful projects to get the information from the user.
This has been a guide to VBA Userform. Here we learn how to create an excel VBA userform to store data from the user along with a practical example and downloadable template. Below you can find some useful excel VBA articles –