查找一个单词并删除下x行

时间:2019-07-12 14:46:36

标签: excel vba

我需要从Excel电子表格中删除一些记录。我希望宏搜索特定名称,并在找到具有该名称的单元格后,删除包含该名称的行和接下来的X行。

到目前为止,我有一部分可以删除某些单词上的单元格内容,但是现在我需要它来清除而不是删除整个行

Range("B2:H100").Replace What:="*Phone", Replacement:="", LookAt:=xlPart, _
                         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                         ReplaceFormat:=False

Range("B2:H100").Replace What:="*Queue", Replacement:="", LookAt:=xlPart, _
                         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                         ReplaceFormat:=False

Range("B2:H100").Replace What:="*2nd Line", Replacement:="", LookAt:=xlPart, _
                         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                         ReplaceFormat:=False

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

Option Explicit

Sub RemoveRowsFindName()

Dim FindRng As Range
Dim xRows As Long
Dim FindWord As String

xRows = 7 ' number of extra rows to remove
FindWord = "Phone"

Set FindRng = Range("B2:H100").Find(What:=FindWord, LookAt:=xlPart, SearchOrder:=xlByRows)
If Not FindRng Is Nothing Then
    Range("A" & FindRng.Row).Resize(1 + xRows, 1).EntireRow.Delete Shift:=xlShiftUp
Else ' word not found in range
    MsgBox "Unable to find " & FindWord & " in range", vbCritical, "Find Error!"
End If

End Sub

答案 1 :(得分:0)

@Shar Rado- 这将是一个较大的脚本的一部分,该脚本旨在清除excel电子表格以使人力资源团队更加透明,我已将您的建议粘贴为:

Dim FindRng As Range
Dim xRows As Long
Dim FindWord As String

xRows = 7
FindWord = "Tony"

Set FindRng = Range("B2:H100").Find(What:=FindWord, LookAt:=xlPart, SearchOrder:=xlByRows)
If Not FindRng Is Nothing Then
    Range("A" & FindRng.Row).Resize(1 + xRows, 1).EntireRow.Delete Shift:=xlShiftUp
End If

但是整个宏的功能与以前相同-不会返回任何错误,也不需要删除。