让我先说一下我在这方面的知识度不高。我在网上找到了一些代码,并根据我的需求量身定制。当我运行它时,它会执行我想要它做的事情(删除一行,其中包含K列中的一个值和J列中的一个值),但它将持续运行而不会结束。当我取消它时,它已经完成了手头的任务,所以我认为它只是停留在循环中。任何和所有帮助都会很棒。
“如果不是rFind就是Nothing那么”是代码Debugs时突出显示的那一行。
Sub DeleteRows()
Sheets("Sheet1").Select
Dim rFind As Range
Dim rFind2 As Range
Dim rDelete As Range
Dim strSearch As String
Dim iLookAt As Long
Dim bMatchCase As Boolean
strSearch = Range("AJ1")
strSearch2 = Range("AK1")
iLookAt = xlWhole
bMatchCase = False
Set rDelete = Nothing
Application.ScreenUpdating = False
With Sheet1.Columns("J:J")
Set rFind2 = .Find(strSearch2, LookIn:=xlValues, LookAt:=iLookAt, SearchDirection:=xlPrevious, MatchCase:=bMatchCase)
If Not rFind2 Is Nothing Then
Do
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
Loop While Not rFind2 Is Nothing
End If
End With
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
你的外圈
Loop While Not rFind2 Is Nothing
将继续循环,因为没有任何东西将rFind2设置为Nothing。看起来好像你想在与rFind相同的地方清除它:
If rFind.Address = rDelete.Address Then
Set rFind = Nothing
Set rFind2 = Nothing
End If