select过滤的数据,并删除最后一个可见的行,不包括标题

我有数据,并做了一些过滤。 现在我想删除整行直到最后一个可见行。 另外,在这种情况下,我不想包含我的标题(第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 

每当使用SpecialCells时,您都需要添加一个error handling程序。

 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 

请尝试下面的内容:

  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 

请给这个尝试…

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

如果您只希望在数据集上应用filter时删除可见的行,请尝试此操作。

 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