8. UserForm – Uso de controles

CheckBox

Private Sub CommandButton1_Click()
If (CheckBox1.Value = True) Then
MsgBox “Ativado”

End If

OptionButton

Private Sub CommandButton1_Click()
If (OptionButton1.Value = True) Then
MsgBox “Ativado”

End If

 

ComboBox

Private Sub btnTeste_Click()
Debug.Print ComboBox1.ListIndex
Debug.Print ComboBox1.Value
End Sub

Private Sub UserForm_Activate()
ComboBox1.AddItem “Rio de Janeiro”
ComboBox1.AddItem “São Paulo”
ComboBox1.AddItem “Minas Gerais”
End Sub

 

Combo Box – For Each

Dim intervalo, celula As Range
Set intervalo = Worksheets(“Plan1”).Range(“a1:a10”)

For Each celula In intervalo
ComboBox1.AddItem celula.Value
Next celula

 

Combo Box – While

Dim i As Integer

i = 1

Do While Cells(i, 1) <> “”
ComboBox1.AddItem Cells(i, 1)
i = i + 1

Loop

 

ListBox

Private Sub UserForm_Activate()

ListBox1.AddItem “VAL1”
ListBox1.AddItem “VAL2”
ListBox1.AddItem “VAL3”

End Sub

ListBox1 x ListBox2

Private Sub CommandButton1_Click()
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
End Sub

Private Sub CommandButton2_Click()
ListBox2.RemoveItem (ListBox2.ListIndex)
End Sub

Private Sub UserForm_Activate()

ListBox1.AddItem Worksheets(“Plan1”).Range(“A2”)
ListBox1.AddItem Worksheets(“Plan1”).Range(“A3”)
ListBox1.AddItem Worksheets(“Plan1”).Range(“A4”)

End Sub