In this article I am going to explain every aspects of VBA Form. User forms in Excel is really nice to see. It looks very fascinating while working with User Forms. You can build a very nice UI (User Interface) using Excel VBA Form. In this Article you are going to learn following things about VBA User Form:
1. How to Create User Form (Creating VBA Form)
2. Showing a VBA Form (Display an Excel Form)
3. How to Initialize a UserForm
4. Hiding a VBA Form
5. Unloading VBA Form
6. Closing VBA Form
7. Example: Creating Data Entry Form using Excel Form
8. Free Download : Data Entry Form using Excel Form
1. How to Create User Form (Creating VBA Form) Creating a Form (Designing a Form) in excel is very easy and interesting. For Designing a Form a single Line Code is not required. Code is required only to make that Form function as per your requirement.
To create a Form, follow the below simple steps:
Step 1. Open your Workbook and Press Alt + F11
Step 2. VB Code Editor will open as below
Step 4. Right Click on Microsoft Excel Object
Step 5. Go to Insert –> User Form
Step 6. In Right Side you can see one Default UserForm1 Created. You can see the ToolBox as well with all the Controls available for the User Form.
Step 7. Using the Tool Box of the User Form you can Drag and Drop what all Controls you want on your form.
Refer the below User form which I have created to show as an example. You can design by your own how you want.
2. Showing a VBA Form (Display an Excel Form): As you have seen we have created the User Form in VBA Code. User form will not be displaying on the Excel Sheet by default. To display the form we need to write a piece of code. This is just a single statement to launch an Existing User Form. You can execute that statement to open User form.
You can add the above statement where you want to launch your user form. UserForm1 is the name of the User Form Control.
3. How to Initialize a VBA Form: Before getting in to technical details lets discuss, what is Initialization of User form?
Initialization of User Form is nothing but to do some default operation while launching the Form. At the time of loading the User Form, we set certain values or any kind of defaulting to any field, comes under Initialization.
Whatever code is there in UserForm_Initialize() Event of User Form, will be executed as soon as User Form will be launched (Shown).
Click on the User Form and you will be taken to the below Private Sub with Initialize Event as shown in Image below:
Private Sub UserForm_Initialize() UserForm1.ComboBox1.Clear With UserForm1.ComboBox1 .AddItem "Junior Engineer" .AddItem "Engineer" .AddItem "Team Lead" .AddItem "Technical Lead" .AddItem "Functional Consultant" .AddItem "Technical Consultant" .AddItem "Project Manager" .AddItem "Delivery Manager" .AddItem "Group Delivery Manager" .AddItem "Unit Head" .AddItem "Regional Head" End With End Sub
4. Closing and Hiding and Unloading a VBA Form: Like windows dialog box User Form also has a by default Cross Button to close the form. It will simply close the User form. There is a difference between Closing and hiding a user form.
Closing a User form will close the User Form. All the value entered in to that form will be cleared (lost) when you launch it again. Whereas on hiding a user form all the vaues entered in to that form will remain there and can be seen again once you launch the user form again. Both the terms (Closing and hiding) are pretty much self explanatory.
Closing or Unloading an Excel User form are same. In VBA Code, to close an Excel Form, Unload keyword is used. This is the reason we call it both either closing a VBA Form or Unloading a VBA Form.
Now you know the about Closing, Hiding and Unloading an Excel UserForm. Now i will show you the VBA syntax for Unloading (Closing) and Hiding with an Example.
Hiding a User Form:
Private Sub UserForm_Click()
Unloading or Closing a User Form:
Unload <User Form Name> OR Unload Me
Private Sub UserForm_Click()
Unload UserForm1 OR Unload Me
5. Example: Creating Data Entry Form using Excel Form: Now its time for doing some hands-on. I have created one Simple Data Entry Form using Excel VBA Form as shown below in Image. If you wish to see the Code and the whole Workbook download it from below:
To Check out more Excel Macro Tutorials, visit Excel Macro Tutorial