Error en código de búsqueda, no me refleja datos en el listbox

Estoy modificando el código de la búsqueda de una base de datos me los mande a labels y a un listbox, se cargan los datos en los label pero en el listbox NO. :C soy nuevo usando el listbox y estoy complicado mucho aquí dejo el código para ver si me ayudan a ver el error

Sub busca2()
'Por.Dante Amor
    Application.ScreenUpdating = False
    Set h1 = PRUEBAS
    Set h2 = Sheets("history")
    'folio = h1.Range("L3").Value
    folio = h1.TextBox1.Value
    If folio = "" Then
        MsgBox "Escribe algo en el textbox1"
        Exit Sub
    End If
    '
    j = 21
    Set r = h2.Columns("A")                 'rango de búsqueda
    Set b = r.Find(folio, lookat:=xlWhole)  'busca folio
    If Not b Is Nothing Then
        celda = b.Address
        Do
         fila = h1.ListBox1.ListIndex
           'Datos del cliente
            H1. Label1.Caption = h2. Cells(b.Row, "A") ' cliente
            H1. Label2.Caption = h2. Cells(b.Row, "B") '
            h1.Label3.Caption = h2.Cells(b.Row, "C")
            'detalle de productos
           H1. ListBox1. List(h1. ListBox1.ListCount - 1, 1) = h2. Cells(b.Row, "D")
           ' ListBox1.List(fila, 1) = h2. Cells(b.Row, "E")
           ' ListBox1.List(fila, 2) = h2. Cells(b.Row, "F")
           ' ListBox1.List(fila, 3) = h2. Cells(b.Row, "G")
            j = j + 1
            Set b = r.FindNext(b)
        Loop While Not b Is Nothing And b.Address <> celda
    Else
        MsgBox "El folio no existe"
    End If
    Application.ScreenUpdating = True
End Sub

aqui las imagenes,

Respuesta
1

Empieza por utilizar una nomenclatura adecuada en tus variables

Set h2 = Sheets("history")

h2 está bien, empieza con h porque se refiere a una "h"oja


Pero esto:

Set h1 = PRUEBAS

No se sabe, a qué te refieres con PRUEBAS, por la imagen se puede ver que te refieres al userform, pero si estás en el userform, no es necesario que lo pongas en una variable objeto.


Para agregar datos al listbox, primero tienes que utilizar el método AddItem, el código quedaría así, pon el código en un commandbutton dentro de tu userform

Private Sub CommandButton1_Click()
'Por.Dante Amor
    Set h2 = Sheets("history")
    folio = TextBox1.Value
    If folio = "" Then
        MsgBox "Escribe algo en el textbox1"
        Exit Sub
    End If
    '
    Set r = h2.Columns("A")                 'rango de búsqueda
    Set b = r.Find(folio, lookat:=xlWhole)  'busca folio
    If Not b Is Nothing Then
        celda = b.Address
        Do
            'Datos del cliente
            Label1.Caption = h2.Cells(b.Row, "A")   ' cliente
            Label2.Caption = h2.Cells(b.Row, "B")   '
            Label3.Caption = h2.Cells(b.Row, "C")
            'detalle de productos
            ListBox1. AddItem h2. Cells(b.Row, "D")
            ListBox1. List(ListBox1.ListCount - 1, 1) = h2. Cells(b.Row, "E")
            ListBox1. List(ListBox1.ListCount - 1, 2) = h2. Cells(b.Row, "F")
            ListBox1. List(ListBox1.ListCount - 1, 3) = h2. Cells(b.Row, "G")
            Set b = r.FindNext(b)
        Loop While Not b Is Nothing And b.Address <> celda
    Else
        MsgBox "El folio no existe"
    End If
End Sub

sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas