Excel VBA: Simple UserForm example.

February 2017


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:

Related


Published by MaxStart. Latest update on April 9, 2015 at 04:12 AM by jak58.
This document, titled "Excel VBA: Simple UserForm example.," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).