删除Excel中的所有行后,包含search文本

我有一个有不同数量的行的电子表格。 在电子表格的有用信息的底部是一个名为“终止”的行,后面是不同数量的行,我对其中没有一个感兴趣。

我怎样才能写一个VBA脚本来search“终止”,并删除所有行后?

我可以这样search“Terminations”:

Cells.Find(What:="Terminations", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate 

我可以像这样删除行:

 Rows("245:246").Select Selection.Delete Shift:=xlUp 

但是,我迄今为止将这两者结合起来的尝试是徒劳的。

试试这个:

 Sub test() Dim rng As Range Dim lastRow As Long 'change Sheet1 to suit With ThisWorkbook.Sheets("Sheet1") 'find Terminations Set rng = .Cells.Find(What:="Terminations", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) 'if Terminations NOT found - exit from sub If rng Is Nothing Then Exit Sub 'find last row If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastRow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else lastRow = 1 End If 'I use lastRow + 1 to prevent deletion "Terminations" when it is on lastrow .Range(rng.Row + 1 & ":" & lastRow + 1).Delete Shift:=xlUp End With End Sub 

如何从这里确定lastRow