Como puedo modificar esta macro para poder agregar más columnas en listbox

Quisiera si me pueden ayudar con un macro que eh estado modificando para hacer una búsqueda en una hoja de más de 15 columnas y reflejar la búsqueda de todas esas columnas, en listbox, dependiendo la búsqueda, además que no eh podido, ver cual es el error en el macro de eliminar un evento de la búsqueda, y poder pasar toda la información que esta en el listbox a una hoja de excel. Quisiera que si me pueden ayudar en alguna de estas, lo que quisiera es la búsqueda.

1 respuesta

Respuesta
1

Disculpen mi macro es:

Dim i, items, xEmpleado

Private Sub btn_Buscar_Click()

If Me.txt_Buscar.Value = Empty Then
MsgBox "Escriba un registro para buscar"
Me.ListBox1.Clear
Me.txt_Buscar.SetFocus
Exit Sub
End If

Me.ListBox1.Clear

items = Range("Tabla1").CurrentRegion.Rows.Count
For i = 2 To items
If LCase(Cells(i, 1).Value) Like "*" & LCase(Me.txt_Buscar.Value) & "*" Then
Me.ListBox1.AddItem Cells(i, 1)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Cells(i, 2)
ElseIf LCase(Cells(i, 2).Value) Like "*" & LCase(Me.txt_Buscar.Value) & "*" Then
Me.ListBox1.AddItem Cells(i, 1)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Cells(i, 2)
ElseIf LCase(Cells(i, 3).Value) Like "*" & LCase(Me.txt_Buscar.Value) & "*" Then
Me.ListBox1.AddItem Cells(i, 1)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Cells(i, 2)
End If
Next i
Me.txt_Buscar.SetFocus
Me.txt_Buscar.SelStart = 0
Me.txt_Buscar.SelLength = Len(Me.txt_Buscar.Text)
Exit Sub

End Sub

Private Sub btn_Eliminar_Click()
Dim Fila As Integer
Dim Final As Integer

If Me.ListBox1.ListIndex = -1 Then
MsgBox "Debe seleccionar un registro"
Exit Sub
End If

'Buscamoos la última fila
Fila = 2
Do While Hoja1.Cells(Fila, 1) <> ""
Fila = Fila + 1
Loop
Final = Fila - 1

If MsgBox("¿Seguro que quiere eliminar este Registro?", vbQuestion + vbYesNo) = vbYes Then
For Fila = 2 To Final
If Hoja1.Cells(Fila, 1) = xEmpleado Then
Hoja1.Cells(Fila, 1).EntireRow.Delete
Exit For
End If
Next
Call btn_Buscar_Click
MsgBox "Registro eliminado", vbInformation + vbOKOnly
Else
Exit Sub
End If
End Sub

Private Sub ListBox1_Click()

'Determino que cuando selecciono un item,
'el valor seleccionado, sea asignado a la variable xEmpleado

items = Me.ListBox1.ListCount
For i = 1 To items - 1
If Me.ListBox1.Selected(i) Then
xEmpleado = Me.ListBox1.List(i)
End If
Next i

End Sub

Private Sub UserForm_Initialize()
'Le digo cuántas columnas
ListBox1.ColumnCount = 3
'Asigno el ancho a cada columna
Me.ListBox1.ColumnWidths = "100 pt;50 pt;50 pt"
'El origen de los datos es la Tabla1
' ListBox1.RowSource = "Tabla1"
End Sub

Private Sub CommandButton1_Click()
Dim Fila As Integer
Dim Final As Integer
Dim i As Integer

For Fila = 1 To 1000
If Hoja2.Cells(Fila, 1) = "" Then
Final = Fila
Exit For
End If
Next

For i = 0 To ListBox1.ListCount - 1
Hoja2.Cells(Final, 1) = ListBox1.List(i, 0)
Hoja2.Cells(Final, 2) = ListBox1.List(i, 1)
Hoja2.Cells(Final, 3) = ListBox1.List(i, 2)
Hoja2.Cells(Final, 4) = ListBox1.List(i, 3)
Final = Final + 1
Next

End Sub

Private Sub CommandButton3_Click()
For t = 0 To ListBox1.ListCount - 1
suma = suma + CDbl(ListBox1.List(t, 1))
Next
MsgBox "la suma de la columna 2 es de: " & suma
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas