User Forms & Controls in VBA
User forms are custom user interface screens that you can develop in VBA to interact with your users. An example user form is shown below.
Learn how to use various form controls & user form features in this section.
What are Form Controls?
Form Controls are objects which you can place onto an Excel Worksheet or User Forms, which give you the functionality to interact with your data.
You can use these controls to help select data. For example, drop-down boxes, list boxes, spinners, and scroll bars are useful for selecting items from a list. Option Buttons and Check Boxes allow selection of various options. Buttons allow execution of VBA code.
You can place form controls on user forms (created from Visual Basic Editor) or inside Excel worksheets.
By adding form controls to user forms, we can tell Excel how the value entered in that should be treated. This is done a special type of macros called as Events.
By adding a control to a worksheet and linking it to a cell, you can return a numeric value for the current position of the control. You can use that numeric value in conjunction with the Offset, Index or other worksheet functions to return values from lists.
Using Excel Form Controls
You can use form controls in 2 contexts – (1) in User Forms (2) in regular Excel worksheets.
Using Form Controls in User Forms
To use form controls, this is the process we need to follow.
- Go to Visual Basic Editor (ALT+F11)
- Insert UserForm
- Select the UserForm, you should see Toolbox with all userform controls.
- If the Toolbox is not displayed, you can enable if from View menu.
- Select any userform control and draw it on the userform.
- Design the form as per your desire.
- Change captions & settings of each control by using Properties window
- Tip: You can adjust properties of multiple controls by selecting them all and then making changes to their properties.
Creating your first UserForm – Demo
Different Form Controls & What they do?
|Button||Push Button||Executes a macro|
|Check Box||Allow selection of non-exclusive options||Multiple On/Off options|
|Combo Box||Drop Down selection Box||Select items from a Drop down list|
|Group Box||Layout element which groups common elements||Nil|
|Label||A Text label||Can be static or linked to a cell|
|List Box||Fixed selection box||Select items from a list|
|Option Button||Allow selection of exclusive options||Exclusive Single On/Off option|
|Scroll bar||Allow Horizontal or Vertical scrolling||Increases or decreases a value by a fixed amount. Can be linked to a cell.|
|Spin Button||Increment/decrement a value by a fixed amount||Increases or decreases a value in steps by a fixed amount. Can be linked to a cell.|
For more information, read Form Controls & how to use them in the context of Excel.
What are Events?
Events are a special type of Macros (VBA instructions) that run whenever user interacts with userform controls. For example, you can write an event to tell Excel what to do whenever user clicks on the submit button. Each userform controls supports various events.
What are Active-x Controls?
Active X controls are like Form Controls on Steroids in that they have a much wider range of properties than Form Controls.
They also have much better ties to VBA in terms of programmability and have a number of events that can be accessed programmatically.
The main limitation of Active X controls are that they use a Microsoft Active X component. This means that if you are sharing your workbook with an Apple Mac user using Excel for Mac these functions wont be available as Active X isn’t available on that Platform.
Using Form Controls inside Excel Workbook
Using Form controls inside Excel is a great way to add interactivity to your workbooks. Below you can see one such example.
We, at Chandoo.org have lots of examples & detailed tutorials on these. Please visit these pages for more information on this.
- Introduction to Form Controls & how to use them in Excel
- Excel Dynamic Charts made with Form Controls [lots of examples]
- Examples on Excel Form Controls
VBA User Form Examples
References on Excel VBA UserForms & Controls
Please read thru these references to learn more about UserForms & Form Controls in Excel VBA.
- Introduction & Overview of Form Controls & Active-x Controls
- Excel User Form Example [excel-vba-easy.com]
- Excel User Form Code Samples [ozgrid.com]
- Extending UsefForm capabilities with Windows API [cpearson.com]
- UserForm Tips [j-walk.com]
- Create an Excel UserForm [contextures.com]