0 calificaciones0% encontró este documento útil (0 votos)
86 vistas2 páginas
Este documento contiene código VBA para crear un formulario de usuario en Excel que permite buscar en una lista desplegable (listbox) mientras se escribe en un cuadro de texto (textbox). El código filtra la lista cuando cambia el texto en los cuadros y establece las propiedades de la lista como el número de columnas y anchos. También incluye código para inicializar el formulario y subrutinas para cerrarlo y mostrarlo.
Descripción original:
Excel 2017 Macro Buscar En Listbox Mientras Se Escribe En Textbox
Título original
Excel 2017 Macro Buscar En Listbox Mientras Se Escribe En Textbox
Este documento contiene código VBA para crear un formulario de usuario en Excel que permite buscar en una lista desplegable (listbox) mientras se escribe en un cuadro de texto (textbox). El código filtra la lista cuando cambia el texto en los cuadros y establece las propiedades de la lista como el número de columnas y anchos. También incluye código para inicializar el formulario y subrutinas para cerrarlo y mostrarlo.
Este documento contiene código VBA para crear un formulario de usuario en Excel que permite buscar en una lista desplegable (listbox) mientras se escribe en un cuadro de texto (textbox). El código filtra la lista cuando cambia el texto en los cuadros y establece las propiedades de la lista como el número de columnas y anchos. También incluye código para inicializar el formulario y subrutinas para cerrarlo y mostrarlo.
'Excel 2017 Buscar En Listbox Mientras Se Escribe En Textbox
'C�digo que se inserta en un userform
Private Sub CommandButton1_Click()
Unload Me End Sub
Private Sub TextBox1_Change()
On Error Resume Next Set b = Sheets("Hoja2") 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:B" & uf Exit Sub End If b.AutoFilterMode = False Me.ListBox1 = Clear Me.ListBox1.RowSource = Clear For i = 2 To uf strg = b.Cells(i, 2).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 Me.ListBox1.ColumnWidths = "30 pt;150 pt" End Sub Private Sub TextBox2_Change() On Error Resume Next Set b = Sheets("Hoja2") uf = b.Range("A" & Rows.Count).End(xlUp).Row If Trim(TextBox2.Value) = "" Then 'Me.ListBox1.List() = b.Range("A2:H" & uf).Value Me.ListBox1.RowSource = "Hoja2!A2:B" & uf Exit Sub End If b.AutoFilterMode = False Me.ListBox1 = Clear Me.ListBox1.RowSource = Clear For i = 2 To uf strg = b.Cells(i, 1).Value If UCase(strg) Like UCase(TextBox2.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 Me.ListBox1.ColumnWidths = "30 pt;150 pt" End Sub
Private Sub UserForm_Initialize()
Dim fila As Long Application.DisplayAlerts = False Application.ScreenUpdating = False Set b = Sheets("Hoja2") uf = b.Range("A" & Rows.Count).End(xlUp).Row uc = b.Cells(1, Columns.Count).End(xlToLeft).Address wc = Mid(uc, InStr(uc, "$") + 1, InStr(2, uc, "$") - 2) With Me.ListBox1 .ColumnCount = 2 .ColumnWidths = "30 pt;150 pt" .RowSource = "Hoja2!A2:" & wc & uf End With Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) On Error GoTo Fin If CloseMode <> 1 Then Cancel = True Fin: End Sub