Macro que filtra y elimina de listbox y hoja
Tengo el siguiente caso y espero alguien me oriente, en un userform tengo un listbox que cargo con la siguiente instrucción.
Private Sub UserForm_Initialize()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Worksheets("Resguardo").Select
Set H1 = Sheets("Resguardo")
With ListBox1
.ColumnCount = 9
.ColumnHeads = True
End With
Cargar
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub3 textbox donde hay que poner fecha inicial y fecha final y el texto a buscar con la siguiente instrucción
Private Sub TextBox1_Change()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error Resume Next
Set b = Sheets("Resguardo")
uf = b.Range("A" & Rows.Count).End(xlUp).Row
If Trim(TextBox1.Value) = "" Then
Me.ListBox1.RowSource = "Resguardo!A2:I" & uf
Exit Sub
End If
b.AutoFilterMode = False
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
dato1 = CDate(TextBox2)
dato2 = CDate(TextBox3)
If dato1 <> Empty Or dato2 <> Empty Then GoTo Rango:
If dato2 < dato1 Then
MsgBox ("La fecha final no puede ser mayor a la fecha inicial"), vbCritical, "AVISO"
Exit Sub
End If
For i = 2 To uf
strg = b.Cells(i, 7).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)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 8) = b.Cells(i, 9)
End If
Next i
Rango:
For i = 2 To uf
strg = b.Cells(i, 7).Value
dato0 = CDate(b.Cells(i, 6).Value)
If UCase(strg) Like UCase(TextBox1.Value) & "*" And dato0 >= dato1 And dato0 <= dato2 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)
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 8) = b.Cells(i, 9)
End If
Next i
End Suby un botón para eliminar del listbox y la hoja después de filtrar con la siguiente macro
Private Sub Eliminartexto() Dim Rango As Range With ListBox1 For x = 0 To .ListCount - 1 If .Selected(x) Then If Rango Is Nothing Then Set Rango = H1.Rows(x + 2) Else Set Rango = Union(Rango, H1.Rows(x + 2)) End If End If Next End With If Not Rango Is Nothing Then Rango.Delete Cargar End If End Sub
Me elimina del listbox el texto seleccionado, pero de la hoja me elimina las primeras filas después del encabezado.