Excel

Example of UserForm Controls

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This is an example of how to use some of the controls that can be put on a UserForm. 

Discussion:

You want to learn how to use a UserForm. This code demonstrates how to use several controls on a UserForm and how to retrieve the value of those controls to use in your code. 

Code:

instructions for use

			

'Place this code in a Module Sub ShowTheForm UserForm1.Show End Sub 'Place this code in a UserForm Option Explicit Private Sub CheckBox1_Click() If UserForm1.CheckBox1.Value = True Then MsgBox "Checkbox1 has been checked." Else MsgBox "Checkbox1 has been unchecked." End If End Sub Private Sub CheckBox2_Click() If UserForm1.CheckBox2.Value = True Then MsgBox "Checkbox2 has been checked." Else MsgBox "Checkbox2 has been unchecked." End If End Sub Private Sub CommandButton1_Click() Dim LastRow As Long LastRow = Range("A65536").End(xlUp).Row + 1 Range("A" & LastRow).Value = TextBox1.Text Range("B" & LastRow).Value = TextBox2.Text Range("C" & LastRow).Value = TextBox3.Text MsgBox "The information in the Text Boxes has been inputed" & _ " into " & ActiveSheet.Name & " in cells A" & LastRow & _ ":C" & LastRow Unload Me End Sub Private Sub CommandButton2_Click() Dim Prompt As String Prompt = "TextBox1: " & vbTab & UserForm1.TextBox1.Text & vbNewLine Prompt = "TextBox2: " & vbTab & UserForm1.TextBox2.Text & vbNewLine Prompt = "TextBox3: " & vbTab & UserForm1.TextBox3.Text & vbNewLine Prompt = Prompt & "CheckBox1: " & vbTab & UserForm1.CheckBox1.Value & vbNewLine Prompt = Prompt & "CheckBox2: " & vbTab & UserForm1.CheckBox2.Value & vbNewLine Prompt = Prompt & "OptionButton1: " & vbTab & UserForm1.OptionButton1.Value & vbNewLine Prompt = Prompt & "OptionButton2: " & vbTab & UserForm1.OptionButton2.Value & vbNewLine Prompt = Prompt & "OptionButton3: " & vbTab & UserForm1.OptionButton3.Value & vbNewLine Prompt = Prompt & "ToggleButton1: " & vbTab & UserForm1.ToggleButton1.Value & vbNewLine MsgBox Prompt End Sub Private Sub ListBox1_Change() MsgBox "You selected " & UserForm1.ListBox1.Text End Sub Private Sub OptionButton1_Click() MsgBox "You selected OptionButton1" End Sub Private Sub OptionButton2_Click() MsgBox "You selected OptionButton2" End Sub Private Sub OptionButton3_Click() MsgBox "You selected OptionButton3" End Sub Private Sub SpinButton1_SpinUp() MsgBox "You pressed the right arrow on the Spin Button." End Sub Private Sub SpinButton1_SpinDown() MsgBox "You pressed the left arrow on the Spin Button." End Sub Private Sub SpinButton2_SpinUp() MsgBox "You pressed the up arrow on the Spin Button." End Sub Private Sub SpinButton2_SpinDown() MsgBox "You pressed the down arrow on the Spin Button." End Sub Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) MsgBox "You changed the text in TextBox1 to " & UserForm1.TextBox1.Text & vbNewLine & _ UserForm1.TextBox1.Text & " has been added to the listbox." UserForm1.ListBox1.AddItem (UserForm1.TextBox1.Text) End Sub Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean) MsgBox "You changed the text in TextBox2 to " & UserForm1.TextBox2.Text & vbNewLine & _ UserForm1.TextBox2.Text & " has been added to the listbox." UserForm1.ListBox1.AddItem (UserForm1.TextBox2.Text) End Sub Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean) MsgBox "You changed the text in TextBox3 to " & UserForm1.TextBox3.Text & vbNewLine & _ UserForm1.TextBox3.Text & " has been added to the listbox." UserForm1.ListBox1.AddItem (UserForm1.TextBox3.Text) End Sub Private Sub ToggleButton1_Click() If UserForm1.ToggleButton1.Value = True Then MsgBox "Toggle Button is down." Else MsgBox "Toggle Button is up." End If End Sub

How to use:

  1. Download the attachment.
 

Test the code:

  1. Open the attached workbook.
  2. Press the Button on Sheet1 to show the UserForm.
  3. After seeing what the UserForm can do, view the code in the VBE.
 

Sample File:

UserFormExample.ZIP 19.63KB 

Approved by mdmackillop


This entry has been viewed 1143 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express