Delete unwanted VBA values

Asked

Viewed 130 times

1

Guys good morning, I have the following macro to run a filter inside a column and bring me a determined value. however, within that same column there are other values, I would like to exclude the values that I will not use, how can I do? follows the Cod.

Set ws3 = ActiveWorkbook.Worksheets("Planilha3")
With ws3
    'Limpa os Autofiltros da Planilha para evitar erros
    If .FilterMode Then
        .ShowAllData
    End If
    'Última Linhada colunaE
    UltimaLinhaE = .Cells(.Rows.Count, "E").End(xlUp).Row
    'Autofiltro
    .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=5, Criteria1:="Cell 01"
End With

1 answer

0


Code

Dim ws3 As Worksheet
Dim UltimaLinhaE As Long
Dim RangeVisivel As Range
Set ws3 = ActiveWorkbook.Worksheets("Planilha3")
With ws3
    'Limpa os Autofiltros da Planilha para evitar erros
    If .FilterMode Then
        .ShowAllData
    End If
    'Última Linhada colunaE
    UltimaLinhaE = .Cells(.Rows.Count, "E").End(xlUp).Row
    'Autofiltro
    .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=1, Criteria1:="<>Cell 01"
    On Error Resume Next
    Set RangeVisivel = .Range(.Cells(1, "E"), .Cells(UltimaLinhaE, "E")).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not RangeVisivel Is Nothing Then
        RangeVisivel.EntireRow.Delete
    End If
End With

Explanation

First the different values of the desired are filtered: .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=1, Criteria1:="<>Cell 01"

Then a range is set with the values of the visible cells: Set RangeVisivel = .Range(.Cells(1, "E"), .Cells(UltimaLinhaE, "E")).SpecialCells(xlCellTypeVisible)

If this Range exists, the entire line is deleted: RangeVisivel.EntireRow.Delete

  • presented an error "Runtime Error 1004 Delete Method of range class failed" If Not Rangevisivel Is Nothing Then Rangevisivel.EntireRow.Delete End If = where the error is

  • You have merged cells?

  • sorry, I do, but they are essential for the presentation of the spreadsheet.... there is another way to perform this filter?

  • Please give me an example?

  • I took the mergers and the error persists

  • True, it is possible to delete with merged cells. It may be the changes made in the code. Hard to know without understanding the code and seeing the data. Try to put Application.DisplayAlerts = False Before Deleter and Application.DisplayAlerts = True after deleting

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.