Excel VBA: Simple UserForm example.

Ask a question

Excel VBA UserForm

In this example we'll create a simple UI form with basic controls and use it to fill an excel sheet with values from it.

now use the toolbox to create the controls on the user form and use the properties windows to modify them to match the picture below:

now double click anywhere inside the user form (care full not to double click on a control just in an empty sot), use Ctrl + A to highlight everything and hit delete.
now write the following code:
Private Sub CommandButton1_Click()
    Cells(2, 1).Value = TextBox1.Text
    Cells(2, 2).Value = ComboBox1.Text
    If OptionButton1.Value = True Then
        Cells(2, 3).Value = "yes"
        Cells(2, 3).Value = "No"
    End If
End Sub

Private Sub UserForm_Initialize()
ComboBox1.AddItem "Internet"
ComboBox1.AddItem "Software"
ComboBox1.AddItem "troubleshooting"
End Sub

now in the Visual Basic windows double click on sheet1 and write the following code
Sub showForm()
End Sub

close the VB window and go back to excel, click on Macros in developer tab

this will assign the keyboard shortcut Ctrl + j to run thee macro

now hit Ctrl + j the user form will appear.
fill in the textbox and other controls and then hit the button on the user form,
and this is the result:
Jean-François Pillou

Jean-François Pillou - Founder of CCM
Better known as Jeff, Jean-François Pillou is the founder of CommentCaMarche.net. He is also CEO of CCM Benchmark and digital director at the Figaro Group.

Learn more about the CCM team