Método buscar en listbox vba

Tengo un listbox donde muestro ciertas columnas(9 columnas), en total son 21, pero solo muestro las que necesito.

El tema es como realizar una búsqueda desde un texbox.

Este es mi código para visualizar los datos en el list box.

Private Sub UserForm_Initialize(): On Error Resume Next
'Dim FILA, FINAL, I As Long
Me.Height = Application.Height
Me.Width = Application.Width

Me.editar.Enabled = False
Me.eliminar.Enabled = False
CommandButton1.Enabled = False

FILA = 4
Do While Hoja2.Cells(FILA, 1) <> Empty
FILA = FILA + 1
Loop
FINAL = FILA - 1

With ListBox1
.ColumnCount = 21
.ColumnWidths = "20;120;80;80;0;0;480;70;70;0;100;0;0;0;0;0;0;60;0;0;0"
End With
Dim Columnas As Integer
Dim MiRango As Range

Set MiRango = ThisWorkbook.Sheets("Datos").Range("A4").CurrentRegion

Columnas = MiRango.Columns.Count

Me.ListBox1.ColumnCount = Columnas
Me.ListBox1.RowSource = MiRango.Address

End sub

////////Estaba intentando acerlo asi: pero no me sale///////////

Dim FILA, FINAL, I As Long
FILA = 2
Do While Hoja1.Cells(FILA, 1) <> Empty
FILA = FILA + 1
Loop
FINAL = FILA - 1
ListBox1.Clear

For I = 2 To FINAL
If UCase(Hoja1.Cells(I, 3)) Like "*" & UCase(TextBox7) & "*" Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = Hoja1.Cells(I, 1)
.List(.ListCount - 1, 1) = Hoja1.Cells(I, 2)
.List(.ListCount - 1, 2) = Hoja1.Cells(I, 3)
.List(.ListCount - 1, 3) = Hoja1.Cells(I, 4)
.List(.ListCount - 1, 4) = Hoja1.Cells(I, 5)
.List(.ListCount - 1, 5) = Hoja1.Cells(I, 6)
.List(.ListCount - 1, 6) = Hoja1.Cells(I, 7)
.List(.ListCount - 1, 7) = Hoja1.Cells(I, 8)
End With
End If
Next I

End Sub

2 respuestas

Respuesta
1

Estás colocando el código en el evento Initialize, o sea al abrir el Userform... y todavía no tienes nada en el textbox. Debieras colocar el código, por ejemplo, en el evento Exit del Textbox, o sea a salir del control luego de escribir el texto por el que quieras filtrar.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim FILA, FINAL, I As Long
'todas las demás instrucciones
End Sub

PD) Te invito a mirar el video 11 de mi canal para mejorar varios detalles de tu código como la búsqueda de la última fila y el uso del nro de hoja,

Sdos y no olvides valorar la respuesta.

Respuesta
1

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas