Macro to delete filtered table row

1

I created a table with data from an account balance and I am making a macro to select only a few values. A macro assignment would filter all zeroed values into one of the columns to exclude the rows of those accounts that had no movement.

After performing the filter, I try to delete only the filtered rows from the table, but I can not.

Would anyone know to let me know if there is a way to do it?

    
asked by anonymous 21.08.2015 / 23:26

1 answer

1

One option would be to apply the algorithm below. The spreadsheet does not need to be filtered to work.

Sub Erase receives a column as parameter and, from line 2, traverses cells that have value = 0 in the current row and column passed as parameter. If the value is = 0, then the current line is deleted.

Sub apagaLinhas(coluna As Integer)
    Dim sheet As Worksheet
    Dim linha As Long

    Set sheet = Worksheets("Plan1")
    linha = 2

    While (sheet.Cells(linha, coluna).Value <> "")
        If (sheet.Cells(linha, coluna).Value = 0) Then
            sheet.Rows(linha).Delete Shift:=xlUp
        Else
            linha = linha + 1
        End If
    Wend
End Sub

Use the function below to test.

Sub teste()
    apagaLinhas (2)
End Sub
    
21.08.2015 / 23:46