我已经搜索过了,我没有找到正确答案或创建代码的运气:
此代码将删除Col D中包含“apple”的整行。
如何在 strSearch 中添加条件?我想添加“香蕉”和“猫”?
Sub Delete_EntireRow_Values()
'
' Delete_EntireRow_Values Macro
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim sFirstAddress As String
strSearch = "apple"
Set rDelete = Nothing
Application.ScreenUpdating = False
With Sheet1.Columns("D:D")
Set rFind = .Find(strSearch, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)
If Not rFind Is Nothing Then
sFirstAddress = rFind.Address
Do
If rDelete Is Nothing Then
Set rDelete = rFind
Else
Set rDelete = Application.Union(rDelete, rFind)
End If
Set rFind = .FindNext(rFind)
Loop While Not rFind Is Nothing And rFind.Address <> sFirstAddress
rDelete.EntireRow.Delete
End If
End With
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
更有效的方法是使用自动过滤器并传递一组单词来删除
Sub DeleteMatchingCriteria()
Application.ScreenUpdating = False
Dim toDelete As Variant
' set the words to delete
toDelete = Array("apple", "cat", "bannana")
Dim colD As Range
Set col = Sheet1.Range("D1:D" & Sheet1.Range("D" & Rows.Count).End(xlUp).Row)
With col
.AutoFilter Field:=1, Criteria1:=toDelete, Operator:=xlFilterValues
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
Sheet1.AutoFilterMode = False
End Sub