Cargar datos desde una hoja a un textbox según valor de otro

Tengo un código copiado de la página SIGNUM en el cual un ListBox que me filtra desde tres textbox.

Con el primero TextBox1 filtro el ID, con el textBox2 el Nombre y con el TextBox el Tipo.

El asunto es que deseo que asignarle un valor al TextBox1 (ID), el textbox2 no se filtre, auto completándose (como buscar en Excel).

Este es el código:

Private Sub TextBox1_Change()
'Declaramos variables
Dim fin As Long, i As Long, n As Long
Dim sCadena_seccion As String
'Filtramos por sección
With Sheets("BBDD")
fin = Application.CountA(.Range("A:A"))
If TextBox1 = "" Then
Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
Exit Sub
End If
Me.TextBox2 = Clear
Me.TextBox3 = Clear
Me.ListBox1.RowSource = Clear
For i = 2 To fin
'AQUI CAMBIAS LA CELDA QUE DARA LA INFORMACION
sCadena_seccion = .Cells(i, 2).Value
If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
n = n + 1
End If
Next
Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
End With
End Sub

Private Sub TEXTBOX2_CHANGE()
Dim fin As Long, i As Long, n As Long
Dim sCadena_seccion As String, sCadena_estudios As String
'Una vez filtrados los datos por sección, filtramos por estudios
With Sheets("BBDD")
fin = Application.CountA(.Range("A:A"))
If TextBox2 = "" Then
Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
Exit Sub
End If

Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
For i = 2 To fin
sCadena_seccion = .Cells(i, 2).Value
sCadena_estudios = .Cells(i, 4).Value
If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" And _
UCase(sCadena_estudios) Like "*" & UCase(TextBox2.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
n = n + 1
End If
Next
Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
End With
End Sub

Private Sub TextBox3_Change()
Dim fin As Long, i As Long, n As Long
Dim sCadena_seccion As String, sCadena_estudios As String, sCadena_idioma As String
'una vez filtrada la información por sección y estudios, filtramos por idioma
With Sheets("BBDD")
fin = Application.CountA(.Range("A:A"))
If TextBox2 = "" Then
Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
Exit Sub
End If
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
For i = 2 To fin
sCadena_seccion = .Cells(i, 2).Value
sCadena_estudios = .Cells(i, 4).Value
sCadena_idioma = .Cells(i, 5).Value
If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" And _
UCase(sCadena_estudios) Like "*" & UCase(TextBox2.Value) & "*" And _
UCase(sCadena_idioma) Like "*" & UCase(TextBox3.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
n = n + 1
End If
Next
Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
End With
End Sub

Private Sub UserForm_Initialize()
'Indicamos el número de columnas que tendrá el listbox
Me.ListBox1.ColumnCount = 7
'Definimos tamaño de los espacios
Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
'Cargamos listbox
Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row

End Sub

Respuesta

1 respuesta más de otro experto

Respuesta
1

No entendí tu petición:

El asunto es que deseo que asignarle un valor al TextBox1 (ID), el textbox2 no se filtre, auto completándose (como buscar en Excel).

Podrías explicar un poco más.

Gracias Dante.

En TextBox1 debo poner el ID del cliente Ej. 5 cuyo nombre en Miguel Vargas datos que me filtra el Textbox2. 

Lo que deseo es que al poner el ID del cliente en el textbox1 el textbox2 se llene automatico. yo filtro manualmente el textbox3 que es el tipo.

Simplemente que no tenga que hacer manualmente el filtro del Textbox2, sino un autocompletar.

Para eso es necesario declarar una variable al principio de todo el código. Cambia tu código por lo siguiente.

Dim cargando
'
Private Sub TextBox1_Change()
    'Declaramos variables
    Dim fin As Long, i As Long, n As Long
    Dim sCadena_seccion As String
    'Filtramos por sección
    With Sheets("BBDD")
        fin = Application.CountA(.Range("A:A"))
        cargando = True     'variable para indicar que está cargando el listbox
        TextBox2.Value = ""
        If TextBox1 = "" Then
            Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
            cargando = False
            Exit Sub
        End If
        Me.TextBox2 = Clear
        Me.TextBox3 = Clear
        Me.ListBox1.RowSource = Clear
        '
        'Busca el textbox1 en la columna "B"
        'si lo encuentra, entonces llena el textbox2
        Set b = .Columns("B").Find(TextBox1.Value, lookat:=xlWhole, LookIn:=xlValues)
        If Not b Is Nothing Then
            TextBox2.Value = .Cells(b.Row, 4).Value
        End If
        '
        For i = 2 To fin
            'AQUI CAMBIAS LA CELDA QUE DARA LA INFORMACION
            sCadena_seccion = .Cells(i, 2).Value
            If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" Then
                Me.ListBox1.AddItem
                Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
                Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
                Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
                Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
                Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
                Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
                Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
                n = n + 1
            End If
        Next
        Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
    End With
    cargando = False    'regresa la variable a false
End Sub
'
Private Sub TextBox2_CHANGE()
    Dim fin As Long, i As Long, n As Long
    Dim sCadena_seccion As String, sCadena_estudios As String
    '
    If cargando Then Exit Sub
    'Una vez filtrados los datos por sección, filtramos por estudios
    With Sheets("BBDD")
        fin = Application.CountA(.Range("A:A"))
        If TextBox2 = "" Then
            Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
            Exit Sub
        End If
        Me.ListBox1 = Clear
        Me.ListBox1.RowSource = Clear
        For i = 2 To fin
            sCadena_seccion = .Cells(i, 2).Value
            sCadena_estudios = .Cells(i, 4).Value
            If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" And _
                UCase(sCadena_estudios) Like "*" & UCase(TextBox2.Value) & "*" Then
                Me.ListBox1.AddItem
                Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
                Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
                Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
                Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
                Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
                Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
                Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
                n = n + 1
            End If
        Next
        Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
    End With
End Sub
'
Private Sub TextBox3_Change()
    Dim fin As Long, i As Long, n As Long
    Dim sCadena_seccion As String, sCadena_estudios As String, sCadena_idioma As String
    'una vez filtrada la información por sección y estudios, filtramos por idioma
    With Sheets("BBDD")
    fin = Application.CountA(.Range("A:A"))
    If TextBox2 = "" Then
    Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
    Exit Sub
    End If
    Me.ListBox1 = Clear
    Me.ListBox1.RowSource = Clear
    For i = 2 To fin
    sCadena_seccion = .Cells(i, 2).Value
    sCadena_estudios = .Cells(i, 4).Value
    sCadena_idioma = .Cells(i, 5).Value
    If UCase(sCadena_seccion) Like "*" & UCase(TextBox1.Value) & "*" And _
    UCase(sCadena_estudios) Like "*" & UCase(TextBox2.Value) & "*" And _
    UCase(sCadena_idioma) Like "*" & UCase(TextBox3.Value) & "*" Then
    Me.ListBox1.AddItem
    Me.ListBox1.List(n, 0) = .Cells(i, 1).Value
    Me.ListBox1.List(n, 1) = .Cells(i, 2).Value
    Me.ListBox1.List(n, 2) = .Cells(i, 3).Value
    Me.ListBox1.List(n, 3) = .Cells(i, 4).Value
    Me.ListBox1.List(n, 4) = .Cells(i, 5).Value
    Me.ListBox1.List(n, 5) = .Cells(i, 6).Value
    Me.ListBox1.List(n, 6) = .Cells(i, 7).Value
    n = n + 1
    End If
    Next
    Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
    End With
End Sub
'
Private Sub UserForm_Initialize()
    'Indicamos el número de columnas que tendrá el listbox
    Me.ListBox1.ColumnCount = 7
    'Definimos tamaño de los espacios
    Me.ListBox1.ColumnWidths = "50pt;30pt;90pt;120pt;50pt;50pt"
    'Cargamos listbox
    Me.ListBox1.RowSource = ("A2:G") & Worksheets("BBDD").Range("A" & Rows.Count).End(xlUp).Row
End Sub

'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas