find特定的文本并删除它上面的三行

我在使用macros时遇到了一些麻烦,我想我会再次请这里的macros观专家来寻求帮助。

我需要帮助find一行中的某些文本,并删除它上面的三行,unlooped。 这是我一直在使用,它不工作。 图片:我需要删除的内容和所需的最终结果。 还有我正在使用的macros

Sub Delete_Rows() Dim x For x = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row To ActiveCell.Row Step -1 If Cells(x, 1) = "TOT" Then Cells(x, 1).EntireRow.Delete Cells(x - 1, 1).EntireRow.Delete Cells(x - 2, 1).EntireRow.Delete Cells(x - 3, 1).EntireRow.Delete End If End Sub 

我也在网上寻找了解决这个常见问题的其他解决scheme,但没有为我工作。 我需要这个macros可以在Excel工作簿的任何页面中使用。

非常感谢你!

如果我理解正确,下面的编辑应该帮助

 Sub Delete_Rows() Dim xRow As Integer Dim strSearch As String strSearch = "TOT" ' Assuming Total is in column C as your picture shows, but you can configure to search anywhere xRow = Range("C" & Rows.Count).End(xlUp).Row Range("$C1:C" & xRow).Select Selection.Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Select Range(ActiveCell.Row & ":" & ActiveCell.Offset(-3, 0).Row).Select Selection.Delete Shift:=xlUp End Sub 

为什么要做一个循环? 为什么不只select汽车的东西?

 Sub Delete_Rows() Const WordToLook = "TOT" Dim RowWord As Long RowWord = Columns(3).Find(WordToLook, LookAt:=xlWhole).Row Rows(RowWord - 3 & ":" & RowWord - 1).Delete End Sub 

这将在工作表中的任何位置find“TOT”,并在其上方删除3行。

 Sub Delete() Dim find As String: find = "TOT" Dim rng As Range Set rng = Sheets("Sheet1").Cells.find(What:=find, After:=Sheets("Sheet1").Cells(1, 1), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True) If Not rng Is Nothing Then rng.EntireRow.Delete rng.Offset(-1).EntireRow.Delete rng.Offset(-2).EntireRow.Delete rng.Offset(-3).EntireRow.Delete End If End Sub