2017-11-13 24 views
0

ユーザー(tbISearch1)によって入力された値がsheet3のC列に表示される各インスタンスを検索する必要がある状況があります。見つかった各インスタンスについて、シート3の列Bの対応する値を取り、これらの値を使用してシート2、列Bを検索し、範囲列A〜Dの関連値を持つリストボックスを作成する必要があります。それはSet rngFind = .FindNext(rngFind)Excel VBAネストされた検索

With Sheet3.Range("C6:C" & lastRow) 
    Set rngFind = .Find(tbISearch1, After:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlPart) 
    ' If value found then set a variable for the address 
    If Not rngFind Is Nothing Then 
     strFirstFind = rngFind.Address 
     ' Add the values to the listbox 
     Do 
      strToFind = rngFind.Offset(0, -1) 
      With Sheet2.Range("B6:B" & lastRow2) 
       Set rngFind2 = .Find(strToFind, After:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlPart) 
       ' If value found then set a variable for the address 
       If Not rngFind2 Is Nothing Then 
        ' Add the values to the listbox 
         If rngFind2.Row > 1 Then 
          lbISearch.AddItem rngFind2.Offset(0, -1) 
          lbISearch.List(lbISearch.ListCount - 1, 1) = rngFind2.Value 
          lbISearch.List(lbISearch.ListCount - 1, 2) = rngFind2.Offset(0, 1) 
          lbISearch.List(lbISearch.ListCount - 1, 3) = rngFind2.Offset(0, 2) 
         End If 
         Set rngFind2 = Nothing 
       End If 
      End With 
      ' Find the next address to add 
      Set rngFind = .FindNext(rngFind) 
     Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind 
    End If 
End With 

のための次のインスタンスを検索しようとしたときのコードは、私は最初のループの後にエラーが表示されたすべてのヘルプ感謝感謝あなたはそのための最も簡単な、ネストされた検索に

+0

'.Find'はネストできません。完了するために最初の '.Find'を実行し、一致のリスト/テーブル/辞書を作る必要があります。次に、リストをトラバースし、残りの処理を行います。または、 '.Finds'をマージして、外側' .Find'の始点を管理するように注意してください。 – AlexP

答えて

0

を.findnext呼び出すことはできません解決策は、もう一度検索を実行するだけですが、 'After'パラメータでセルアドレスを使用して、次に見つかったセルに移動します。

Set rngFind = .Find(rngFind, After:=Range(rngFind.Address)) 
+0

夢見てくれてありがとう – user3673417

関連する問題