Excel VBA: Simple UserForm example.

October 2016

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:

Related :

This document entitled « Excel VBA: Simple UserForm example. » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.