User Forms are one of the very important features in Excel Macro. It provides and an interface to the user for interaction with the workbook. It has many controls which makes user’s life easy for entering the data from excel in a specific manner or reading the particular data from the excel.
Getting Started : Create a Blank User Form
- To open or create a new user form, follow the steps below
- Open an Excel file
- Press “Alt+F11” to open the Microsoft Visual Basic Editor.
- Make Project Explorer and Property Window visible from “view menu” (If not already visible)
- Select the Project from the Project Explorer.
- Form “Insert” menu, select “User Form”
- You will see “UserForm1” under “Form” section
- One the right side you will see a Userform .
- Make All Controls (Tool box) visible from “View menu” if already not displayed.
- Look for all the Properties of the User Form in Property Window.
- Give a Proper Name and Captionto the UserForm1,
- Select the Userform1
- Go to the Property Window,
- Change the Name Property to “FirstForm”
- Change the Caption Property to “My First User Form”
16. Add Controls to the User form
- Select the control from the Tool Box and place it in the User Form.( here Command Button is selected)
- Select or Single Click on the Command Button from the Tool Box.
- Now Click on the UserForm, a new CommandButton will be created.
- Change the Button Name and Caption name in the Property Window
- Double Click on the button to view the Click function of the Button(Actually when you double click it first time, then only “Click” function will be created.)
- Inside the function write Msgbox “Yeaaaahhhhh , I got Clicked”.Private Sub
Private Sub FirstButton_Click()
MsgBox “Yeaaaahhhhh , I got Clicked“
NOTE : To Navigate Between “Code Window” and “UserForm Design Window” KeyBorad Shortcut is [Control]+[Tab]
17. Now your User Form is ready to run. All you have to do is assign this User Form to a macro.
Macro to Open the UserForm
- Select the VBA Project from Project Explorer
- Form “Insert” menu, select “Module”
- A new Module will be create in the Project Explorer under Modules folder
- Click on the Module 1, Code window will be displayed on right side.
- Create a Sub-Routine say with name “Sub Sumit”
- Display the UserForm inside the Sub-Routine using Show() function.
- Sub Sumit()
- Create a button in Excel WorkSheet and assign the Sub-Routine to it ( Click Here)
- Click on the Button on Excel
- UserForm ( FirstForm) will be displayed.
- Click on the CommandButton(“Click Me”)