Bva excel ¿Como hacer un filtrado en un listBox con tres TextBox?

Tengo un formulario que contiene un lixtbox y tres Textbox. El lixtbox me muestra los registros en mi base de datos. Lo que necesito es que cuando escriba en el Textbox1 me filtre los datos que estoy colocando, posteriormente cuando escriba el valor en el lixtBox2 me filtre igual los datos que busco pero que matenga los que ya filtro en el lixbox1 y con el tercero que haga lo mismo pero que me mantenga los valores que ya filtre en los dos textBox.

Este código lo implemente es los 3 y si filtra. Pero hace un filtrado en general...

Private Sub TextBox1_Change()
On Error Resume Next
Set b = Sheets("Formulario")
uf = b.Range("A" & Rows.Count).End(xlUp).Row
If Trim(TextBox1.Value) = "" Then
'Me.ListBox1.List() = b.Range("A2:H" & uf).Value
'Me.ListBox1.RowSource = "Hoja2!A2:H" & uf

End If
b.AutoFilterMode = False
Me.ListBox1.Clear
Me.ListBox1.RowSource = Clear
'Adiciona un item al listbox reservado para la cabecera
UserForm1.ListBox1.AddItem
For i = 2 To uf
strg = b.Cells(i, 1).Value
If UCase(strg) Like UCase(TextBox1.Value) & "*" Then
Me.ListBox1.AddItem b.Cells(i, 1)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = b.Cells(i, 2)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = b.Cells(i, 3)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = b.Cells(i, 4)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = b.Cells(i, 5)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = b.Cells(i, 6)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = b.Cells(i, 7)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 7) = b.Cells(i, 8)
End If
Next i

'Carga los datos de la cabecera en listbox
For ii = 0 To 8
UserForm1.ListBox1.List(0, ii) = Sheets("Formulario").Cells(1, ii + 1)
Next ii
'Me.ListBox1.ColumnWidths = "20 pt;70 pt;180 pt;80 pt;60 pt;60 pt;60 pt;60pt"
End Sub

1 respuesta

Respuesta
1

Te anexo el código actualizado

Private Sub TextBox1_Change()
    Call Filtrar
End Sub
Private Sub TextBox2_Change()
    Call Filtrar
End Sub
Private Sub TextBox3_Change()
    Call Filtrar
End Sub
'
Sub Filtrar()
'Act Por Dante Amor
    Set h = Sheets("Formulario")
    uf = h.Range("A" & Rows.Count).End(xlUp).Row
    If h.AutoFilterMode Then h.AutoFilterMode = False
    ListBox1.Clear
    'Carga los datos de la cabecera en listbox
    ListBox1.AddItem
    For ii = 0 To 8
        ListBox1.List(0, ii) = h.Cells(1, ii + 1)
    Next ii
    '
    For i = 2 To uf
        str1 = h.Cells(i, 1).Value
        str2 = h.Cells(i, 2).Value
        str3 = h.Cells(i, 3).Value
        If TextBox1.Value = "" Then text1 = str1 Else text1 = TextBox1.Value
        If TextBox2.Value = "" Then text2 = str2 Else text2 = TextBox2.Value
        If TextBox3.Value = "" Then text3 = str3 Else text3 = TextBox3.Value
        If UCase(str1) Like UCase(text1) & "*" And _
           UCase(str2) Like UCase(text2) & "*" And _
           UCase(str3) Like UCase(text3) & "*" Then
            ListBox1.AddItem h.Cells(i, 1)
            ListBox1. List(ListBox1.ListCount - 1, 1) = h.Cells(i, 2).Value
            ListBox1. List(ListBox1.ListCount - 1, 2) = h.Cells(i, 3).Value
            ListBox1. List(ListBox1.ListCount - 1, 3) = h.Cells(i, 4).Value
            ListBox1. List(ListBox1.ListCount - 1, 4) = h.Cells(i, 5).Value
            ListBox1. List(ListBox1.ListCount - 1, 5) = h.Cells(i, 6).Value
            ListBox1. List(ListBox1.ListCount - 1, 6) = h.Cells(i, 7).Value
            ListBox1. List(ListBox1.ListCount - 1, 7) = h.Cells(i, 8).Value
        End If
    Next i
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