2

我今天遇到了这个有趣的问题。我在另一个循环中有一个循环,两者都Find用于不同的目的。发生的事情是Find在内部循环中使用会破坏Find外部循环。我猜只会记住一个搜索实例。有没有办法解决这个问题或者这是一个设计问题?

这是我的代码的一些缩短版本。

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

注意.Findfor 循环中的第二次使用。

有什么方法可以在某种临时变量中保留第一次搜索并在此之后将其恢复?

非常感谢。

4

1 回答 1

5

当您运行FindNext(MSDN for FindNext)时,它会自动使用与what上次调用相同的方法Find,即使用于不同的范围也是如此。

为了纠正这一点,而不是使用

Set active_result = lst_rapports.FindNext(active_result)

采用

Set active_result = lst_rapports.Find(rap_choisi,active_result)

于 2012-10-09T21:12:03.217 回答