【发布时间】:2014-09-16 13:49:20
【问题描述】:
我想创建一个 for 循环来检查我拥有的工作表中的所有行,并希望此代码能够删除某些列中包含指定内容的行(即,如果列 K 包含“June”删除该行。有没有办法对此进行编码?
*编辑 我有代码可以在一列中搜索条件,但现在我需要它来根据两列中的数据搜索和删除行。即如果K列的数据匹配单元格AJ1(已经有)并且J列的数据匹配AK1,则删除这些行。
我的代码是这样的:
Sub DeleteRows()
Sheets("Sheet1").Select
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim iLookAt As Long
Dim bMatchCase As Boolean
strSearch = Range("AJ1")
iLookAt = xlWhole
bMatchCase = False
Set rDelete = Nothing
Application.ScreenUpdating = False
With Sheet1.Columns("K:K")
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
结束子
【问题讨论】:
-
是的,有办法。尝试编写它,然后返回您的代码并描述您遇到的任何问题。或者尝试在此处搜索已经提出和回答的许多类似问题:stackoverflow.com/search?q=vba+delete+rows
标签: excel delete-row vba