Excel VBA - exit for loop Excel VBA - exit for loop vba vba

Excel VBA - exit for loop


To exit your loop early you can use Exit For

If [condition] Then Exit For


Another way to exit a For loop early is by changing the loop counter:

For i = 1 To 10    If i = 5 Then i = 10Next iDebug.Print i   '11

For i = 1 To 10    If i = 5 Then Exit ForNext iDebug.Print i   '5


The first answer given with the following is indeed i.m.o. best practice:

if i = 0 then exit for

However, this is also an option:

Sub some()Count = 0End_ = ThisWorkbook.Sheets(1).Range("B1047854").End(xlUp).RowWhile Count < End_ And Not ThisWorkbook.Sheets(1).Range("B" & Count).Value = "Artikel"    Count = Count + 1    If ThisWorkbook.Sheets(1).Range("B" & Count).Value = "Artikel" Then        ThisWorkbook.Sheets(1).Range("A1:Z" & Count - 1).EntireRow.Delete Shift:=xlToLeft    End IfWendEnd Sub


matomo