你将如何删除Excel中的行,使范围(select,ActiveCell.SpecialCells(xlLastCell))。select不会select现在空的行

问题是我有一个函数删除包含特定文本的行:

Sub DeleteRowsContaining(text As String) Dim Firstrow As Long Dim Lastrow As Long Dim Lrow As Long With ActiveSheet 'Set the first and last row to loop through Firstrow = .UsedRange.Cells(2).Row Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row 'We loop from Lastrow to Firstrow (bottom to top) For Lrow = Lastrow To Firstrow Step -1 'We check the values in the A column With .Cells(Lrow, "A") If Not IsError(.Value) Then If .Value = text Then .EntireRow.Delete 'This will delete each row with the Value of text 'in Column A, case sensitive. End If End With Next Lrow End With End Sub 

在执行这个代码块之后,我想select剩余的文本,但是,从列末尾删除的行仍然会在使用

 Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select 

命令。 真正的问题是使用下一个命令时发生的错误

 Selection.End(xlDown).Offset(1, 0).Select 

当使用该命令时,如果select了两个以上的空行,则会引发错误。 在这一点上,我不知所措,因为我可以轻松dynamic地检查数据范围的结尾的所有方法都返回空白单元格的位置。

我不知道是否有另一种方式来删除不保留一个单元格的行可以select,如果它有数据,或者如果有一种方法来修剪选定的空行,当Excel似乎相信行有东西那里。

完成删除不需要的行后,要select仍在运行的行:

 Sub SelectTheRest() Dim N As Long N = Cells(Rows.Count, "A").End(xlUp).Row Range("A1:A" & N).EntireRow.Select End Sub