选择筛选数据并删除最后一个可见行,不包括标题

时间:2017-09-28 11:45:17

标签: excel vba excel-vba

我有数据并做了一些过滤。现在我想删除直到最后一行的整行。另外,在这种情况下,我不想包含我的标题(第5行)。我不确定如何解决以下代码:

Dim row1 As Variant
row1 = Rows(5).Offset(1, 0)
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Rows("row1:" & lastrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete

3 个答案:

答案 0 :(得分:0)

请试一试......

Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A6:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete

如果您只想在数据集上应用过滤器时删除可见行,请尝试此操作。

Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
If ActiveSheet.FilterMode Then
    On Error Resume Next
    Range("A6:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If

答案 1 :(得分:0)

请尝试以下方法:

    Sub test()

    Dim lastrow As Long
    Dim rng As Range

    lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

    Set rng = Rows("6:" & lastrow)

    rng.Delete Shift:=xlUp

    End Sub

答案 2 :(得分:0)

每当使用SpecialCells时,您都需要添加一个错误处理程序。

Sub DeleteVisibleRows()
    With ActiveSheet
        On Error Resume Next
        .Range("A5", .Range("A" & .Rows.Count).End(xlUp)).Offset(1).EntireRow _
        .SpecialCells(xlCellTypeVisible).Delete
        On Error GoTo 0
    End With
End Sub