title: Exiting While Loop from Recursive Function
body: <p>I've written this code to delete duplicate rows in a column of an Excel spreadsheet. I'm not sure how to, or if it is possible to, exit the while loop from within the function itself. I don't want to add a second condition to the loop (such as <code>Counter &lt; 100</code>) because I don't want it to run more than needed.</p>
  
  <pre><code>Sub Deletion()
  Dim Cond As Boolean
  Dim Counter As Integer
  Cond = True
  Counter = 2
      While Cond = True
      RecDel (Counter)
      Counter = Counter + 1
      Wend
  End Sub
  
  Function RecDel(Varii As Integer)
  Set CurrentCell = Workbooks("Duplicate.xls").Sheets("Sheet1").Cells(Varii, 2)
          If CurrentCell.Value = CurrentCell.Offset(1, 0).Value And CurrentCell.Offset(1, 0).Value &lt;&gt; "" Then
                  Workbooks("Duplicate.xls").Sheets("Sheet1").Rows(Varii + 1).Delete
                  RecDel (Varii) 'Repeats deletion until this row and the next row are different
          ElseIf CurrentCell.Offset(1, 0).Value = "" Then
              Cond = False  'This can't change the global variable and break the loop
          Else
          End If
  End Function
  </code></pre>