When you want to input data into excel cells, you can input it directly. But when there are so many rows in your worksheet, you may need to scroll a lot of rows to input the new row data.
With the excel user forms, you can input data with forms which likes the usually used web forms on web pages. And it makes input data into excel rows easy. This article will tell you how to create a user form in excel using VBA and how to use it to input row data in an excel worksheet.
1. How To Create A User Form In Excel Using VBA.
- Click the Developer tab in the excel ribbon, then click the Visual Basic item in the Code group.
- Then it will open the Microsoft Visual Basic for Applications window.
- If you can not find the Developer tab, please read the article How To Enable Developer Tab And Write VBA Source Code In Excel to learn more.
- Click the Insert —> UserForm menu item in the Microsoft Visual Basic for Applications window top menu bar to create a user form.
- Then you can see there is a new user form created in the VBA window left side, the form name is UserForm1, and it is located in the VBAProject / Forms folder.
- Click UserForm1, it will show the user form designer window on the right side.
- Right-click UserForm1, and then you can toggle the form GUI designer and source code editor by clicking the View Code / View Object menu item in the popup menu list.
- We click the View Object sub-menu to show the UserForm designer window on the right side.
2. How To Add Controls On The User Form.
- Open the user form designer, when you click the form, it will show the ToolBox window beside the form.
- There are some controls on the form window.
- In this example, we add 2 label text controls ( User Name, and Password), 1 input text box for username, 1 password input text box for the password, and 2 buttons ( Regist, Reset ).
- You can just click the related components in the ToolBox, then drag it to the form body.
- After you add a related control in the user form, you can set the control’s Name, Caption, and Text properties in the control’s properties list under the VBAProject panel.
3. How To Add Source Code When User Click The Buttons On User Form.
- Double click the Regist button, then it will open the source code editor window, and add the below function in it.
Private Sub BtnRegist_Click() End Sub
- The above function will be invoked when the user clicks the Regist button.
- The complete source code of the above function is as below, when the user clicks the Regist button, it will input the user name and password into the excel worksheet with the tab name Sheet1. You can see the code comments for a detailed explanation.
Private Sub BtnRegist_Click() ' Get the row number of the first not empty row from excel the last row to top. notEmptyRowNumber = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row ' Assign the user name value in the user form to the A column in the first empty row from the top. Range("a" & notEmptyRowNumber + 1) = IdUserName.Value ' Assign the password value in the user form to the B column in the first empty row from the top. Range("b" & notEmptyRowNumber + 1) = IdPassword.Value ' Empty username & password input text value in the user form. IdUserName.Value = "" IdPassword.Value = "" End Sub
- Add the below function to process the click event for the button Reset.
Private Sub BtnReset_Click() ' Reset username & password input text value in the user form. IdUserName.Value = "" IdPassword.Value = "" End Sub
- Now you can run the user form and input the user name and password, when you click the Regist button, you can see the added user name and password in the excel sheet1.
4. How To Show The User Form In Excel.
- After you create and write the source code for the user form in excel, you can use it in your excel worksheet.
- But before you can use it, you need to display it on the screen. We will use a VBA macro to display the UserForm on the screen as below.
- First, click the Insert —> Module menu item in the Microsoft Visual Basic for Applications window.
- Then it will add a module file Module1 in the VBAProject / Modules folder on the window left side.
- Write the below source code in the module file, the below source file will create a macro with the name Regist_User_Account. It will show the user form when the macro is invoked.
Sub Regist_User_Account() UserForm1.Show End Sub
- When the above macro is invoked, it will show the UserForm1.
5. How To Use The User Form In Excel WorkSheet.
- Now, it is time to use the UserForm in your excel worksheet.
- Open the excel worksheet, and click the Developer tab.
- Click the Insert —> Button ( Form Control ) in the Controls group.
- Then it will pop up a dialog window with the title Assign Macro.
- Select the above macro that is created in section 4 from the Macro name list.
- Click the OK button, then it will close the Assign Macro window and add a button on the sheet.
- You can change the button size, and change the button text to Regist User Account.
- Now when you click this button, it will call the macro and display the user form, then you can input user account data in the user form.