如果单元格包含指定的单词,则删除行

时间:2013-10-28 19:21:39

标签: vba

如果A列中的任何单元格包含单词“Total”,是否有人知道要删除整行的vba代码?例如,A38包含“Total”,删除整行。下个月,单元格A44包含单词“total”,删除整行。等等......谢谢!

1 个答案:

答案 0 :(得分:0)

给下面的测试运行,看它是否接近你想要的。

Sub TestDeleteRows()
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim iLookAt As Long
Dim bMatchCase As Boolean

strSearch = "Total"

iLookAt = xlPart 'Change to xlWhole if the entire cell must equal search string
bMatchCase = False  'Change to True if you want search to be case sensitive

Set rDelete = Nothing

Application.ScreenUpdating = False

With Sheet1.Columns("A:A")
    Set rFind = .Find(strSearch, LookIn:=xlValues, LookAt:=iLookAt, SearchDirection:=xlPrevious, MatchCase:=bMatchCase)
    If Not rFind Is Nothing Then
        Do
            Set rDelete = rFind
            Set rFind = .FindPrevious(rFind)
            If rFind.Address = rDelete.Address Then Set rFind = Nothing
            rDelete.EntireRow.Delete
        Loop While Not rFind Is Nothing
    End If
End With
Application.ScreenUpdating = True
End Sub