title: How can I limit the number of loops in a Do While loop?
body: <p>So I've written a function that that uses text-box inputs to search for corresponding values in another sheet. The problem is if it doesn't find a match it goes into an infinite loop. Can I limit the loops so it doesn't crash?
  If there is another solution rather than limiting loops, I'm all ears.
  Here's what I'm working with: </p>
  
  <pre><code>Function Most_Recent_Deployment(Label1 As String, Label2 As String, Label3 As String) As Long
      Dim all_rows As Range
      Dim row As Range
      Dim LastCell As Range
      Dim LastCellRowNumber As Long
      Set LastCell = Sheet7.Cells(Sheet7.Rows.Count, "A").End(xlDown).End(xlUp)
      LastCellRowNumber = LastCell.row + 1
      Set row = Sheet7.Range("A:A").Find(Label1, LookIn:=xlValues, After:=Cells(LastCellRowNumber, "A"), SearchDirection:=xlPrevious)
      Do While row.row &gt; 1
  
          If (Sheet7.Cells(row.row, 2).Text = Label2) And (Sheet7.Cells(row.row, 3).Text = Label3) Then
              Most_Recent_Deployment = row.row
              Exit Function
          End If
          LastCellRowNumber = row.row
          Set row = Sheet7.Range("A:A").Find(Label1, LookIn:=xlValues, After:=Cells(LastCellRowNumber, "A"), SearchDirection:=xlPrevious)
  
      Loop
      Most_Recent_Deployment = 0
  End Function
  </code></pre>