Saberexcel - o site das macros Exemplo de procedimentos do Aplicativo Microsoft Excel VBA, contendo oito(8) exemplos de Listbox, muito bom para usarmos em nossos projetos e trabalhos do dia-a-dia. Espero que isso possam ajudá-los. Fiquem com Deus, E_Marcondes
Imagem do exemplo da listbox 1
Option Explicit
Private Sub cmbOK_Click()
If ListBox1.ListIndex = -1 Then MsgBox "Nenhuma lista valor selecionado!", vbCritical Exit Sub End If
Cells(2, 1).Value = ListBox1.Value
Unload Me End Sub
Private Sub cmFechar_Click() Unload Me End Sub
Private Sub UserForm_Initialize() Dim vaManad As Variant
vaManad = Array("Janeiro", "Fevereiro", "Março", "Abril", _ "Maio", "Junho", "Julho", "Agosto", _ "Setembro", "Outubro", "Novembro", _ "Dezembro")
With ListBox1 .Clear .List = vaManad .ListIndex = -1 .TextAlign = fmTextAlignCenter End With
End Sub
'...............................'
Abaixo Imagem e procedimentos do exemplo da Listbox2
Option Explicit
Private Sub cmbOK_Click() If ListBox2.ListIndex = -1 Then MsgBox "Nennhum valor da lista foi selecionado!", vbCritical, "Saberexcel - o site das macros" Exit Sub End If
Cells(2, 3).Value = ListBox2.Value
Unload Me End Sub
Private Sub UserForm_Initialize() Dim vGrupos, iValores As Variant Dim stLista() As String Dim i As Integer
vGrupos = VBA.Array("AA", "BB", "CC") iValores = VBA.Array("10", "20", "30")
ReDim stLista(0 To UBound(vGrupos), 0 To 1)
For i = 0 To UBound(vGrupos) stLista(i, 0) = vGrupos(i) stLista(i, 1) = iValores(i) Next i
With ListBox2 .Clear .BoundColumn = 2 .ColumnCount = 2 .List = stLista .ListIndex = -1 End With End Sub
Private Sub mdFechar_Click() Unload Me End Sub
'...............................'
Abaixo Imagem e procedimentos do exemplo da Listbox3
Option Explicit
Private Sub cmbOK_Click() Dim i As Integer Dim rnCell As Range
Set rnCell = Sheets("ListBox3").Range("A1")
If ListBox3.ListIndex = -1 Then MsgBox "Nenhum valor da lista foi selecionado!", vbCritical, "Saberexcel - o site das macros" Exit Sub End If
For i = 0 To ListBox3.ListCount - 1 If ListBox3.Selected(i) Then _ rnCell.Offset(i, 0).Value = ListBox3.List(i) Next i
Unload Me End Sub
Private Sub cmdFechar_Click() Unload Me End Sub
Private Sub UserForm_Initialize() Dim vLista, iValores As Variant Dim stLista() As String Dim i As Integer
vLista = VBA.Array("AA", "BB", "CC") iValores = VBA.Array("10", "20", "30")
ReDim stLista(0 To UBound(vLista), 0 To 1)
For i = 0 To UBound(vLista) stLista(i, 0) = vLista(i) stLista(i, 1) = vaAndra(i) Next i
With ListBox3 .Clear .BoundColumn = 2 .ColumnCount = 2 .List = stLista .ListIndex = -1 .MultiSelect = fmMultiSelectMulti .TextAlign = fmTextAlignCenter End With
End Sub
'...............................' Abaixo Imagem e procedimentos do exemplo da Listbox4
Option Explicit
Private Sub cmbOK_Click() Dim i As Integer Dim rnCell As Range
Set rnCell = Sheets("ListBox4").Range("A1")
If ListBox2.ListIndex = -1 Then MsgBox "Insira o valor na lista!", vbCritical Exit Sub End If
rnCell.Value = ListBox2.Value
Unload Me End Sub
Private Sub ListBox1_Click() Dim vaJan, vaFev, VaMar, vaAbril As Variant
vaJan = VBA.Array("1", "2", "3") vaFev = VBA.Array("10", "20", "30") VaMar = VBA.Array("100", "200", "300") vaAbril = VBA.Array("1000", "2000", "3000")
Select Case ListBox1.Value Case "Janueiro" ListBox2.List = vaJan Case "Fevereiro" ListBox2.List = vaFev Case "Março" ListBox2.List = VaMar Case "Abril" ListBox2.List = vaAbril End Select
End Sub
Private Sub UserForm_Initialize() Dim vaLista1 As Variant
vaLista1 = VBA.Array("Janeiro", "Fevereiro", "Março", "Abril")
With ListBox1 .Clear .List = vaLista1 End With End Sub
Private Sub cmdFechar_Click() Unload Me End Sub
'...............................' Abaixo Imagem e procedimentos do exemplo da Listbox5
Option Explicit
Private Sub cmbOK_Click() Dim i, j As Integer Dim rnCell As Range
Set rnCell = Sheets("ListBox5").Range("A1")
If ListBox5.ListIndex = -1 Then MsgBox "Nenhum valor foi seleciona nesta lista!", vbCritical, "Saberexcel - o site das macros" Exit Sub End If
j = 0 For i = 0 To ListBox5.ListCount - 1 If ListBox5.Selected(i) Then j = j + 1 rnCell.Offset(j, 0).Value = ListBox5.List(i, 0) rnCell.Offset(j, 1).Value = ListBox5.List(i, 1) End If Next i
Unload Me End Sub
Private Sub cmdFechar_Click() Unload Me End Sub
Private Sub UserForm_Initialize() Dim iValores, vaAndra As Variant Dim stLista() As String Dim i As Integer
iValores = VBA.Array("AA", "BB", "CC") vaAndra = VBA.Array("10", "20", "30")
ReDim stLista(0 To UBound(iValores), 0 To 1)
For i = 0 To UBound(iValores) stLista(i, 0) = iValores(i) stLista(i, 1) = vaAndra(i) Next i
With ListBox5 .Clear .BoundColumn = 2 .ColumnCount = 2 .List = stLista .ListIndex = -1 .MultiSelect = fmMultiSelectMulti End With
End Sub
Private Sub cmdFechar_Click() Unload Me End Sub
'...............................' Abaixo Imagem e procedimentos do exemplo da Listbox6
Option Explicit
Private Sub UserForm_Initialize() Dim vCelula, vIntervalo As Range
Set vIntervalo = Worksheets("Listbox6").Range("B2:G2")
For Each vCelula In vIntervalo ListBox6.AddItem vCelula.Value Next vCelula
End Sub
Private Sub cmbOK_Click() Unload Me End Sub
'...............................' Abaixo Imagem e procedimentos do exemplo da Listbox7
Private Sub UserForm_Initialize() Dim rnLista, rnCell As Range Dim wkData As Worksheet
Set wkData = ThisWorkbook.Sheets("ListBox7")
Set rnLista = Union(wkData.Range("A2:A4"), wkData.Range("E2:E4"))
For Each rnCell In rnLista ListBox7.AddItem rnCell.Value Next rnCell
End Sub
Private Sub cmbOK_Click() Unload Me End Sub
'...............................' Abaixo Imagem e procedimentos do exemplo da Listbox8
Option Explicit
Private Sub cmbOK_Click() Dim iDados, vLista As Integer Dim i, j As Integer Dim vTemp As String
iDados = 0 vLista = ListBox8.ListCount - 1
For i = iDados To vLista - 1 For j = i + 1 To vLista If ListBox8.List(i) > ListBox8.List(j) Then vTemp = ListBox8.List(j) ListBox8.List(j) = ListBox8.List(i) ListBox8.List(i) = vTemp End If Next j Next i
End Sub
Private Sub cmdFechar_Click() Unload Me End Sub
Private Sub UserForm_Initialize() Dim vMeses As Variant vMeses = Array("Jan", "Fev", "Mar", "Abr", _ "Mai", "Jun", "Jul", "Ago", _ "Set", "Out", "Nov", _ "Dez")
With ListBox8 .Clear .List = vMeses .ListIndex = -1 End With
End Sub
Aprenda tudo sobre o Aplicativo Microsoft Excel VBA - com SaberExcel
|