How to use VBA Form Controls with Examples

blog img

In VBA, you can create user forms to interact with users by placing controls like buttons, text boxes, combo boxes, and others on the form. Here’s a breakdown of the most common controls used in a VBA form, along with a description and examples:

1. Label

  • Description: Displays text or captions that the user cannot edit.
  • Common Properties: Caption, Font, Visible
  • Example:

vba

Label1.Caption = "Enter your name:"
Label1.Font.Size = 12

2. TextBox

  • Description: Allows users to input text.
  • Common Properties: Text, Value, Enabled, Locked
  • Example:

vba

TextBox1.Text = "Default Text"
MsgBox TextBox1.Value ' Display the entered text

3. CommandButton

  • Description: A clickable button that triggers actions.
  • Common Properties: Caption, Enabled, Visible
  • Example:

vba

CommandButton1.Caption = "Submit"
Private Sub CommandButton1_Click()
    MsgBox "Button Clicked!"
End Sub

4. ComboBox

  • Description: A dropdown list that allows the user to select an option.
  • Common Properties: List, Value, Enabled, Text
  • Example:

vba

ComboBox1.AddItem "Option 1"
ComboBox1.AddItem "Option 2"
MsgBox ComboBox1.Value ' Display selected item

5. ListBox

  • Description: Displays a list of items from which the user can select.
  • Common Properties: List, Value, Selected
  • Example:

vba

ListBox1.AddItem "Item 1"
ListBox1.AddItem "Item 2"
If ListBox1.Selected(0) Then
    MsgBox "Item 1 selected"
End If

6. CheckBox

  • Description: A box that can be checked or unchecked.
  • Common Properties: Value, Caption
  • Example:

vba

If CheckBox1.Value = True Then
    MsgBox "Checked"
Else
    MsgBox "Not Checked"
End If

7. OptionButton (Radio Button)

  • Description: Allows users to select one option from a set of choices.
  • Common Properties: Value, Caption, Enabled
  • Example:

vba

If OptionButton1.Value = True Then
    MsgBox "Option 1 selected"
End If

8. Frame

  • Description: Groups controls together, typically OptionButtons.
  • Common Properties: Caption, Visible, Enabled
  • Example:

vba

Frame1.Caption = "Choose an Option"

9. MultiPage

  • Description: Creates a multi-page form, where each page can have its own controls.
  • Common Properties: Pages, Value
  • Example:

vba

MultiPage1.Pages(0).Caption = "Page 1"
MultiPage1.Pages(1).Caption = "Page 2"

10. ToggleButton

  • Description: A button that toggles between two states (pressed and unpressed).
  • Common Properties: Value, Caption
  • Example:

vba

If ToggleButton1.Value = True Then
    MsgBox "Toggle On"
Else
    MsgBox "Toggle Off"
End If

11. SpinButton

  • Description: A button that increments or decrements a value.
  • Common Properties: Value, Min, Max
  • Example:

vba

SpinButton1.Min = 0
SpinButton1.Max = 10
Label1.Caption = SpinButton1.Value

12. ScrollBar

  • Description: Allows the user to scroll through a range of values.
  • Common Properties: Min, Max, Value
  • Example:

vba

ScrollBar1.Min = 1
ScrollBar1.Max = 100
Label1.Caption = ScrollBar1.Value

13. Image

  • Description: Displays an image.
  • Common Properties: Picture, Height, Width
  • Example:

vba

Image1.Picture = LoadPicture("C:\Path\To\Image.jpg")

14. RefEdit

  • Description: Allows users to select a range of cells from the worksheet.
  • Common Properties: Value
  • Example:

vba

MsgBox RefEdit1.Value ' Display selected cell range

15. TabStrip

  • Description: Displays a set of tabs for users to switch between different views.
  • Common Properties: Tabs, SelectedItem
  • Example:

vba

TabStrip1.Tabs.Add "Tab 1"
TabStrip1.Tabs.Add "Tab 2"

Common Form Events:

1. Initialize: Triggered when the form is loaded.

vba

Private Sub UserForm_Initialize()
    MsgBox "Form is loading"
End Sub

2. Activate: Triggered when the form is activated.

vba

Private Sub UserForm_Activate()
    MsgBox "Form is active"
End Sub

3. Click Event: Triggered when a button is clicked (used with CommandButton).

vba

Private Sub CommandButton1_Click()
    MsgBox "You clicked me!"
End Sub

Note: These are just a few of the most common controls and how they are typically used in a VBA form. Each control has various properties and events that can be used to customize its functionality.

Share your thoughts

Your email address will not be published. All fields are required.

Related post

  1. How to Use Buttons in VBA: A Step-by-Step Guide

    In VBA (Visual Basic for Applications), buttons are typically used…

  1. What is VBA – how to VBA learn easy way.

    1. What is VBA? VBA (Visual Basic for Applications) is…

Popular post

  1. Eclipse IDE – Create New Java Project.

    Opening the New Java Project…

  1. How to start the project in android studio

    Android Studio Open the Android…

  1. How to use ACOSH function in excel

    The ACOSH function returns the…

  1. Complete Header tags in html – easy to learn

    H tags can be used…

  1. Best features in Python programme – easy to learn

    Python is the most widely…