在VBA中有多个Range.Find()
今天我遇到了这个有趣的问题。 我有一个循环里面的另一个循环,并使用Find
不同的目的。 发生什么事是在内部循环中使用Find
将外部循环中的Find
拧紧。 我猜测, Excel只保留一个search实例的内存。 有没有办法解决这个问题,或者这是一个devise问题?
这是我的代码的一个缩短版本。
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
注意for循环中第二次使用.Find
。
有什么方法可以保留某种临时variables的第一个search,然后恢复它?
非常感谢。
当您运行FindNext
(MSDN for FindNext)时 ,即使用于不同的范围,它也自动使用与Find
上的最后一次调用相同的what
。
为了纠正这个,而不是使用
Set active_result = lst_rapports.FindNext(active_result)
使用
Set active_result = lst_rapports.Find(rap_choisi,active_result)