Excel VBA - exit for loop

CustomX picture CustomX · Feb 23, 2012 · Viewed 839.2k times · Source

I would like to exit my for loop when a condition inside is met. How could I exit my for loop when the if condition has been met? I think some kind of exit at the end of my if statement, but don't know how that would work.

Dim i As Long
For i = 1 To 50
    Range("B" & i).Select
    If Range("B" & i).Value = "Artikel" Then
        Dim temp As Long
        temp = i
    End If
Next i
Range("A1:Z" & temp - 1).EntireRow.Delete Shift:=xlToLeft

Answer

Dan picture Dan · Feb 23, 2012

To exit your loop early you can use Exit For

If [condition] Then Exit For