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"
    Else
        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()
    UserForm1.Show
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
showForm()

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

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team