title: Excel VBA: FindNext in nested loops
body: <p>I am trying to create a loop using the <code>.Find</code> function within another loop which is already using <code>.Find</code>. I want to search for strings that I have saved in an array. </p>
  
  <p>For example, these are the string values saved in the array <code>strItem</code> in Sheet1.</p>
  
  <pre><code>"unit1", "unit2", "unit3"
  </code></pre>
  
  <p>I would like to search them one by one from Sheet2. Sheet2 looks like this:</p>
  
  <pre><code>unit1
  unit2
  unit3
  unit1.pdf
  text1
  subject1
  subject2
  subject3
  text2
  =========
  unit2.pdf
  text1
  subject1
  subject2
  subject3
  text2
  =========
  unit3.pdf
  text1
  subject1
  subject2
  subject3
  text2
  =========
  </code></pre>
  
  <p>After searching for <code>"unit1.pdf"</code>, I search all cells below it for "subject", and get cell values of subject1, 2, and 3. The search for "subject" cells should stop at the next cell which contains "====". </p>
  
  <p>Next I search for <code>"unit2"</code>, and if found search for "subject" cells under it as before. Again, stop at the cell containing "====". And so on.</p>
  
  <p>In my code, what I am trying to do was </p>
  
  <ul>
  <li>Search for the string "unit". </li>
  <li>Use its <code>.row</code> as the range to start searching for "subject". </li>
  <li>Return all subjects until the cell contains <code>"===="</code>. <strong>This is a part of my code that I can't really make to work</strong>.</li>
  </ul>
  
  <p>Code:</p>
  
  <pre><code>Wb2.Sheets("Sheet2").Activate
  With Wb2.Sheets("Sheet2").Range("A1:A1048575")
      For Each strItem In arrExcelValues
          myStr = strItem &amp; ".pdf"
          Set p = .Find(What:=myStr, LookIn:=xlValues, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
          If Not p Is Nothing Then
              firstAddress = p.Address
              Do
                  myStr2 = p.row
                  strStart = "A" &amp; myStr2
                  strEnd = "A1048575"
  
                  With Wb2.Sheets("Sheet2").Range(strStart, strEnd)
                      Set p1 = .Find(What:="Subject", LookIn:=xlValues, _
                                     LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
                      If Not p1 Is Nothing Then
                          firstAddress = p1.Address
                          Do
                              myStr2 = myStr2 + 1
                              If p1.Offset(myStr2, 0).Value = "====" Then
                                  Exit Do
                              Else
                                  MsgBox p1.Value &amp; strItem
                              End If
                              Set p1 = .FindNext(p1)
                          Loop While Not p1 Is Nothing And p1.Address &lt;&gt; firstAddress
                      Else
                          MsgBox "Not found"
                      End If
                  End With
                      Set p = .FindNext(p)
              Loop While Not p Is Nothing And p.Address &lt;&gt; firstAddress
          Else
              MsgBox "Not found"
          End If
      Next
  End With
  </code></pre>