Como cargar datos en un listbox desde otro libro

Me gustaria hacer un filtro en un listbox trayendo los datos desde otro libro de excel diferente a donde se encuentra el listbox. En internet encontre un codigo que me trae datos de otro libro y llena el listbox pero solo trae la primera fila, no las demas.

No se mucho de programacion y por eso me gustaria pedirle su ayuda al respecto puesto que aqui en este foro encontre un tema similar pero al momento de probar el codigo me salian muchos errores y no supe como resolverlos.

Muchas gracias

https://drive.google.com/file/d/0Byit3bberPfVVUdxZWI2LWFBRlE/view?usp=sharing

1 respuesta

Respuesta
1

H o l a:

Para realizar el código, me puedes enviar tus 2 libros, el libro que tiene el listbox y el otro libro con los datos.

Me dices cómo se llama tu formulario

También me dices en qué hoja y en qué columnas está la información que se va a cargar.

También mencionaste un filtro, dime cómo vas a filtrar la información, pon un ejemplo, con cuál dato y con cuál columna vas a filtrar.

Entre más detallado describas tu ejemplo, más práctico será realizar el código.

Mi correo [email protected]

En el asunto del correo escribe tu nombre de usuario “Brayan osorio” y el título de esta pregunta.

Hola dam, ya te he enviado los archivos al correo.

Muchas gracias por tu disposición.

H o l a:

Te anexo todo el código con los cambios:

Dim l2, h2
'
Private Sub cmbEncabezado_Change()
'Cambia el TextBox con cada cambio en el Combo
    Me.lblFiltro = "Filtro por " & Me.cmbEncabezado.Value
End Sub
'
Private Sub CommandButton4_Click()
'Por.Dante Amor
    Call Filtrar
    txtFiltro1.SetFocus
End Sub
'
Private Sub Filtrar()
'Por.Dante Amor
    If h2.FilterMode = True Then ActiveSheet.ShowAllData
    If txtFiltro1 = "" Then Exit Sub
    ListBox1.Clear
    col = Me.cmbEncabezado.ListIndex + 1
    If col = 0 Then Exit Sub
    n = h2.Name
    If Contie Then cad = "*" & txtFiltro1 & "*"
    If Empiece Then cad = txtFiltro1 & "*"
    If Termine Then cad = "*" & txtFiltro1
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        If UCase(h2.Cells(i, col)) Like UCase(cad) Then
            ListBox1. AddItem h2.Cells(i, 1)
            ListBox1. List(ListBox1.ListCount - 1, 1) = h2.Cells(i, 2)
            ListBox1. List(ListBox1.ListCount - 1, 2) = h2.Cells(i, 3)
            ListBox1. List(ListBox1.ListCount - 1, 3) = h2.Cells(i, 4)
            ListBox1. List(ListBox1.ListCount - 1, 4) = h2.Cells(i, 5)
            ListBox1. List(ListBox1.ListCount - 1, 5) = h2.Cells(i, 6)
            ListBox1. List(ListBox1.ListCount - 1, 6) = h2.Cells(i, 7)
            ListBox1. List(ListBox1.ListCount - 1, 7) = h2.Cells(i, 8)
            ListBox1. List(ListBox1.ListCount - 1, 8) = h2.Cells(i, 9)
            ListBox1. List(ListBox1.ListCount - 1, 9) = h2. Cells(i, 10)
        End If
    Next
End Sub
'
Private Sub txtFiltro1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'mayuscula texto de filtrado
    KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
'
Private Sub UserForm_Initialize()
'Act.Por.Dante Amor
'Dar formato al ListBox y traer los encabezados de la tabla
    Application.ScreenUpdating = False
    Call OcultarHojas
    Set l1 = ThisWorkbook
    Set l2 = Workbooks.Open("maestro de codigos.xlsx")
    l1.Activate
    '
    Set h2 = l2.Sheets("MAESTRO")
    For i = 1 To 9
        Me.Controls("Label" & i) = h2.Cells(1, i).Value
    Next i
    '
    With Me
        .ListBox1.ColumnCount = 9
        .ListBox1.ColumnWidths = "56 pt;50 pt;200 pt;40 pt;70 pt;100 pt;60 pt;60 pt;60 pt;50 pt"
        .cmbEncabezado.List = Application.Transpose(h2.Range("A1").Resize(1, 9).Value)
        .cmbEncabezado.ListStyle = fmListStyleOption
    End With
    Me.cmbEncabezado.Text = "DESCRIPCION"
    Application.ScreenUpdating = True
End Sub
'
Private Sub CommandButton2_Click()
'Cerrar formulario
    'ThisWorkbook.Application.Visible = True
    'Call Mostrarlibro
    l2.Close
    Unload Me
End Sub
Sub Mostrarlibro()
    Dim Hoja As Worksheet
    For Each Hoja In Worksheets
        If Hoja.CodeName <> "Hoja1" Then
            Hoja.Visible = xlSheetVisible
      End If
    Next Hoja
End Sub
'
Sub OcultarHojas()
    Dim Hoja As Worksheet
    For Each Hoja In Worksheets
        If Hoja.CodeName <> "Hoja1" Then
            Hoja.Visible = xlSheetVeryHidden
      End If
    Next Hoja
End Sub

':)
':)

Muchísimas gracias por tu ayuda, totalmente lo que estaba Buscando.

Aquí dejo la corrección realizada por Dante Amor

Private Sub UserForm_Initialize()
'Act.Por.Dante Amor
'Dar formato al ListBox y traer los encabezados de la tabla
Application.ScreenUpdating = False
Call OcultarHojas
Set l1 = ThisWorkbook
ruta = l1.Path & "\"
ChDir (ruta)
Set l2 = Workbooks.Open(ruta & "MAESTEAM ACTUALIZADO ENERO 25 DE 2016.xlsx")
l1.Activate
'
Set h2 = l2.Sheets("MAESTRO")
For i = 1 To 9
Me.Controls("Label" & i) = h2.Cells(2, i).Value
Next i
'
With Me
.ListBox1.ColumnCount = 9
.ListBox1.ColumnWidths = "56 pt;50 pt;200 pt;40 pt;70 pt;100 pt;60 pt;60 pt;60 pt;50 pt"
.cmbEncabezado.List = Application.Transpose(h2.Range("A2").Resize(1, 9).Value)
.cmbEncabezado.ListStyle = fmListStyleOption
End With
Me.cmbEncabezado.Text = "DESCRIPCION"
Application.ScreenUpdating = True


End Sub

La pregunta no admite más respuestas

Más respuestas relacionadas