title: Excel Macro - Using find in a Do Loop
body: <p>Within a Do Loop that I have, I want to use the 'find' function, but it seems to keep bumping me out of the loop after one iteration when I try this.</p>
  
  <p>FYI, I'm not great at VB, better with web code eg: PHP etc</p>
  
  <p>If I comment out the row that begins sfFamilyCol = wsSFDC.Rows(1)...... then the loop will run as many times as it should.  With that row in there, it doesnt throw an error but it only goes through once.</p>
  
  <p>I'm wondering if there is some special way of handling .Find within a loop?  Or something else I have no idea about?  Any help appreciated...</p>
  
  <pre><code>Option Explicit
  
  Sub mapTags()
  
  Dim wsMP As Worksheet: Set wsMP = ActiveWorkbook.Sheets("MP")
  Dim wsSFDC As Worksheet: Set wsSFDC = ActiveWorkbook.Sheets("SFDC")
  Dim wsMap As Worksheet: Set wsMap = ActiveWorkbook.Sheets("Mapping")
  Dim wsUp As Worksheet: Set wsUp = ActiveWorkbook.Sheets("Upload")
  Dim wsCol As Worksheet: Set wsCol = ActiveWorkbook.Sheets("MP_Columns")
  Dim wsFmt As Worksheet: Set wsFmt = ActiveWorkbook.Sheets("Tag Name Formats")
  
  Dim i As Long, j As Long, k As Long
  Dim SFDCrow As Long, SFDCID As String, sfCol As Long
  Dim MPID As String, mpTagGroup As String, mpTagName As String, mpTagCol As Long, mapTagName As String
  Dim sfTagFamily As String, sfTagGroup As String, sfTagName As String, sfFamilyCol As Long, sfGroupCol As Long
  Dim oRange As Range, aCell As Range, bCell As Range
  
  Application.ScreenUpdating = False
  
  ''Get Contact Record
  For i = 2 To 2 Step -1  'Change i to 25000 later...
  
  MPID = wsMP.Cells(i, 1).Value
  sfCol = wsSFDC.Columns(2).Find(What:=MPID, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Row
  'MsgBox sfCol
  
  ''Go Through each MP Contact Tag Colum
  For k = 10 To 1 Step -1
      mpTagGroup = wsCol.Cells(k, 1).Value
      If Not mpTagGroup = "" Then
          mpTagCol = wsMP.Rows(1).Find(What:=mpTagGroup, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
  
          '' Get the Tag Name
          mpTagName = wsMP.Cells(i, mpTagCol).Value
          If Not mpTagName = "" Then
  
              ''Get the Mapped SFDC Tags
              Set oRange = wsMap.Columns(1)
              Set aCell = oRange.Find(What:=mpTagGroup, LookIn:=xlValues, _
              LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
              MatchCase:=False, SearchFormat:=False)
              'MsgBox mapTagGroup
  
              If Not aCell Is Nothing Then
                  Set bCell = aCell
                  'FoundAt = aCell.Row
                  Do
                      Set aCell = oRange.FindNext(After:=aCell)
  
                      If Not aCell Is Nothing Then
                          If aCell.Row = bCell.Row Then Exit Do
                          'FoundAt = FoundAt &amp; ", " &amp; aCell.Row
                          mapTagName = wsMap.Cells(aCell.Row, 2).Value
                          If mapTagName = mpTagName Then
                              sfTagFamily = wsMap.Cells(aCell.Row, 4).Value
                              sfTagGroup = wsMap.Cells(aCell.Row, 5).Value
                              sfTagName = wsMap.Cells(aCell.Row, 6).Value
  
                              'MsgBox aCell &amp; " " &amp; mapTagName &amp; ": " &amp; sfTagFamily &amp; " " &amp; sfTagGroup &amp; " " &amp; sfTagName
                              MsgBox sfTagFamily
  
                              ''Set the SDDC TAG FAMILY to TRUE
                              sfFamilyCol = wsSFDC.Rows(1).Find(What:=sfTagGroup, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
                              MsgBox sfFamilyCol
  
                          End If
                      Else
                          Exit Do
                      End If
                  Loop
              End If
  
              'MsgBox "The Search String has been found in these rows: " &amp; FoundAt
  
  
          End If
      End If
  Next k
  Next i
  
  End Sub                    
  </code></pre>