Saltar al contenido
PROGRAMAR EN VBA MACROS DE EXCEL

Lenar listbox con Add Item o RowSource


.

Existen varios post dedicados a tratar listbox como ser listbox de varias columnas dependiente de combobox, buscar en listbox mientras se escribe en textbox, combobox depende de otro combobox y rellena listbox entre otros, en esta oportunidad se tratará el tema de llenar listbox con Add Item o RowSource.

Antes de seguir recomiendo leer un excelente libro sobre Excel que te ayudará operar las planillas u hojas de cálculo, haz click acá, si quieres aprender sobre Excel, en inglés, entonces debes hacer click here. Si lo que necesitas es aprender o profundizar sobre la programación de macros con VBA este es unos de los mejores cursos on line que he visto en internet.

  
Llenar un listbox se puede realizar a través o con el comando Add Item o asignando un rango a Row Source; cual usar va a depender de la cantidad de datos que se recuperan, aunque por mi experiencia si son una gran cantidad de datos es más rápido asignar el rango a RowSource.

También se pueden recorrer los datos e ir incorporando a cada uno de los productos del listado con el comando Add Item.

El vídeo que sigue muestra una explicación más detallada y gráfica de la macro presentada, recomiendo observar para una más fácil comprensión de la macro; suscribe a nuestra web desde la parte superior derecha de la página ingresando tu mail y a nuestro canal de You Tube para recibir en tu correo vídeos explicativos sobre macros interesantes, como  por ejemplo formulario que crea un listado de todas las hojas para poder luego seleccionarlasbuscar en listbox mientras escribes en textboxordenar hojas libro excel por su nombreconectar Excel con Access y muchos ejemplos más.


if (payload.eventType == ‘subscribe’) {
// Add code to handle subscribe event.
} else if (payload.eventType == ‘unsubscribe’) {
// Add code to handle unsubscribe event.
}
if (window.console) { // for debugging only
window.console.log(‘YT event: ‘, payload);
}
}

Código que se inserta en un módulo

Sub muestra1()
UserForm1.Show

End Sub

Código que se inserta en un formulario

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub TextBox1_Change()
‘On Error Resume Next
‘Dim articulos As Worksheet
‘Set articulos = Sheets(«ARTI»)
‘uf = articulos.Range(«B» & Rows.Count).End(xlUp).Row
‘uc = articulos.Cells(9, Columns.Count).End(xlToLeft).Address
‘wc = Mid(uc, InStr(uc, «$») + 1, InStr(2, uc, «$») – 2)

‘For Each arti In articulos.Range(«D21:» & wc & uf)
   ‘ If arti Like TextBox1 & «*» Then
      ‘  ListBox1 = Clear
     ‘   ListBox1.AddItem arti
    ‘    ListBox1.List(ListBox1.ListCount – 1, 1) = arti.Row ‘Fila
    
  ‘     a = ListBox1.ListCount
  ‘      ListBox1.AddItem
   ‘     ListBox1.List(a, 0) = articulos.Cells(arti, 2)
    ‘    ListBox1.List(a, 1) = b.Cells(fila, 3)
     ‘   ListBox1.List(a, 2) = b.Cells(fila, 4)
     ‘   ListBox1.List(a, 3) = b.Cells(fila, 5)
     ‘   ListBox1.List(a, 4) = b.Cells(fila, 6)
     ‘   ListBox1.List(a, 5) = b.Cells(fila, 7)
     ‘   ListBox1.List(a, 6) = b.Cells(fila, 8)
     ‘   ListBox1.List(a, 7) = b.Cells(fila, 9)
     ‘   ListBox1.List(a, 8) = b.Cells(fila, 10)
     ‘   ListBox1.List(a, 9) = b.Cells(fila, 11)
    
 ‘   End If
‘Next arti
‘Me.ListBox1 = Clear
uf = Sheets(«ARTI»).Range(«B» & Rows.Count).End(xlUp).Row
If Trim(TextBox1.Value) = «» Then
     Me.ListBox1.List() = Sheets(«ARTI»).Range(«B9:J» & Sheets(«ARTI»).Range(«A» & Rows.Count).End(xlUp).Row).Value
     Me.ListBox1.RowSource = «ARTI!B21:K» & uf
   Exit Sub
End If
Sheets(«ARTI»).AutoFilterMode = False
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
For i = 10 To uf
   strg = Sheets(«ARTI»).Cells(i, 3).Value
   If strg Like UCase(TextBox1.Value) & «*» Then
       Me.ListBox1.AddItem Sheets(«ARTI»).Cells(i, 2)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 1) = Sheets(«ARTI»).Cells(i, 3)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 2) = Sheets(«ARTI»).Cells(i, 4)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 3) = Sheets(«ARTI»).Cells(i, 5)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 4) = Sheets(«ARTI»).Cells(i, 6)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 5) = Sheets(«ARTI»).Cells(i, 7)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 6) = Sheets(«ARTI»).Cells(i, 8)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 7) = Sheets(«ARTI»).Cells(i, 9)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 8) = Sheets(«ARTI»).Cells(i, 10)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 9) = Sheets(«ARTI»).Cells(i, 11)
    End If
Next i
Me.ListBox1.ColumnWidths = «50 pt;180 pt;180 pt;60 pt;40 pt;40 pt;40 pt;40 pt;40 pt;40 pt»

Exit Sub

Errores:
   MsgBox «No se encuentra.», vbExclamation, «EXCELeINFO»

End Sub
Private Sub TextBox2_Change()
‘On Error Resume Next
‘Dim articulos As Worksheet
‘Set articulos = Sheets(«ARTI»)
‘uf = articulos.Range(«B» & Rows.Count).End(xlUp).Row
‘uc = articulos.Cells(9, Columns.Count).End(xlToLeft).Address
‘wc = Mid(uc, InStr(uc, «$») + 1, InStr(2, uc, «$») – 2)

‘For Each arti In articulos.Range(«D21:» & wc & uf)
   ‘ If arti Like TextBox1 & «*» Then
      ‘  ListBox1 = Clear
     ‘   ListBox1.AddItem arti
    ‘    ListBox1.List(ListBox1.ListCount – 1, 1) = arti.Row ‘Fila
    
  ‘     a = ListBox1.ListCount
  ‘      ListBox1.AddItem
   ‘     ListBox1.List(a, 0) = articulos.Cells(arti, 2)
    ‘    ListBox1.List(a, 1) = b.Cells(fila, 3)
     ‘   ListBox1.List(a, 2) = b.Cells(fila, 4)
     ‘   ListBox1.List(a, 3) = b.Cells(fila, 5)
     ‘   ListBox1.List(a, 4) = b.Cells(fila, 6)
     ‘   ListBox1.List(a, 5) = b.Cells(fila, 7)
     ‘   ListBox1.List(a, 6) = b.Cells(fila, 8)
     ‘   ListBox1.List(a, 7) = b.Cells(fila, 9)
     ‘   ListBox1.List(a, 8) = b.Cells(fila, 10)
     ‘   ListBox1.List(a, 9) = b.Cells(fila, 11)
    
 ‘   End If
‘Next arti
‘Me.ListBox1 = Clear
‘Me.ListBox1 = Clear
‘Me.ListBox1.RowSource = Clear
uf = Sheets(«ARTI»).Range(«B» & Rows.Count).End(xlUp).Row
If Trim(TextBox2.Value) = «» Then
     Me.ListBox1.List() = Sheets(«ARTI»).Range(«B9:J» & Sheets(«ARTI»).Range(«A» & Rows.Count).End(xlUp).Row).Value
     Me.ListBox1.RowSource = «ARTI!B21:K» & uf
   Exit Sub
End If
Sheets(«ARTI»).AutoFilterMode = False
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
For i = 10 To uf
   strg = Sheets(«ARTI»).Cells(i, 4).Value
   If strg Like UCase(TextBox2.Value) & «*» Then
       Me.ListBox1.AddItem Sheets(«ARTI»).Cells(i, 2)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 1) = Sheets(«ARTI»).Cells(i, 3)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 2) = Sheets(«ARTI»).Cells(i, 4)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 3) = Sheets(«ARTI»).Cells(i, 5)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 4) = Sheets(«ARTI»).Cells(i, 6)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 5) = Sheets(«ARTI»).Cells(i, 7)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 6) = Sheets(«ARTI»).Cells(i, 8)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 7) = Sheets(«ARTI»).Cells(i, 9)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 8) = Sheets(«ARTI»).Cells(i, 10)
       Me.ListBox1.List(Me.ListBox1.ListCount – 1, 9) = Sheets(«ARTI»).Cells(i, 11)
    End If
Next i
Me.ListBox1.ColumnWidths = «50 pt;180 pt;180 pt;60 pt;40 pt;40 pt;40 pt;40 pt;40 pt;40 pt»

Exit Sub

Errores:
   MsgBox «No se encuentra.», vbExclamation, «EXCELeINFO»

End Sub

Private Sub Frame2_Click()

End Sub

Private Sub UserForm_Initialize()
Dim fila As Long
On Error Resume Next
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set a = Sheets(«Hoja1»)
uf = a.Range(«B» & Rows.Count).End(xlUp).Row
uc = a.Cells(9, Columns.Count).End(xlToLeft).Address
wc = Mid(uc, InStr(uc, «$») + 1, InStr(2, uc, «$») – 2)
With Me.ListBox1
    .ColumnCount = 10
    .ColumnWidths = «50 pt;180 pt;180 pt;60 pt;40 pt;40 pt;40 pt;40 pt;40 pt;40 pt»
    .RowSource = «Hoja1!A2:» & wc & uf
End With

With Me.ListBox2
    .ColumnCount = 10
    .ColumnWidths = «50 pt;180 pt;180 pt;60 pt;40 pt;40 pt;40 pt;40 pt;40 pt;40 pt»
    .RowSource = «Hoja1!A2:» & wc & uf
    .List() = a.Range(«A2:H» & uf)
End With

‘ Me.ListBox1.List() = Sheets(«ARTI»).Range(«B9:J» & Sheets(«ARTI»).Range(«A» & Rows.Count).End(xlUp).Row).Value
     

‘Me.ListBox1.ColumnCount = 10
‘Me.ListBox1.RowSource = «ARTI!B21:» & wc & uf

‘ListBox1.Clear

fila = 2
While a.Cells(fila, 1) <> Empty
        aa = ListBox3.ListCount
        ListBox3.AddItem
        ListBox3.List(aa, 0) = a.Cells(fila, 1)
        ListBox3.List(aa, 1) = a.Cells(fila, 2)
        ListBox3.List(aa, 2) = a.Cells(fila, 3)
        ListBox3.List(aa, 3) = a.Cells(fila, 4)
        ListBox3.List(aa, 4) = a.Cells(fila, 5)
        ListBox3.List(aa, 5) = a.Cells(fila, 6)
        ListBox3.List(aa, 6) = a.Cells(fila, 7)
        ListBox3.List(aa, 7) = a.Cells(fila, 8)
fila = fila + 1
Wend
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
On Error GoTo Fin
If CloseMode <> 1 Then Cancel = True
Fin:
End Sub

Si te fue de utilidad puedes INVITARME UN CAFÉ y de esta manera ayudar a seguir manteniendo la página, CLICK para descargar en ejemplo en forma gratuita.


.

If this post was helpful INVITE ME A COFFEE and so help keep up the page, CLICK to download free example.

Si te gustó por favor compártelo con tus amigos
If you liked please share it with your friends