App*_*Pie 2 excel vba excel-2007
我今天遇到了这个有趣的问题.我在另一个循环中有一个循环,它们都Find用于不同的目的.发生的事情是Find在内部环路中使用螺钉拧紧Find外部环路.我猜excel只记忆一个搜索实例.有没有办法解决这个问题,还是设计问题?
这是我的代码的一些缩短版本.
Sub Main()
'Some boring stuff
Set lst_rapports = Worksheets("mappingTRANSIT").range("lst_rapports")
Set first_result = lst_rapports.Find(rap_choisi)
Set active_result = first_result
Sheets("req01").Unprotect "shoobidoowap"
If Not first_result Is Nothing Then
' ...
Do
Sheets("req01").Select
' ...
For i = 0 To 4
Set rubrique_cell = range("E:E").Find(rub(i))
If Not rubrique_cell Is Nothing Then
' ...
End If
Next i
' Yet more boring stuff...
Set active_result = lst_rapports.FindNext(active_result)
Loop Until active_result.Address = first_result.Address
Else
MsgBox "Impossible de trouver """ & rap_choisi & """ !"
End If
Sheets("req01").Protect "shoobidoowap"
End Sub
Run Code Online (Sandbox Code Playgroud)
注意.Find在for循环中的第二次使用.
有没有什么方法可以保留第一次搜索某种临时变量并在之后恢复它?
非常感谢.
当您运行FindNext(FindNext的MSDN)时,它会自动使用与what上次调用相同的内容Find,即使用于其他范围也是如此.
要纠正这个问题,而不是使用
Set active_result = lst_rapports.FindNext(active_result)
使用
Set active_result = lst_rapports.Find(rap_choisi,active_result)