Excel VBA - exit for loop

前端 未结 3 1635
深忆病人
深忆病人 2020-11-29 17:43

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 th

3条回答
  •  迷失自我
    2020-11-29 18:22

    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 = 0
    End_ = ThisWorkbook.Sheets(1).Range("B1047854").End(xlUp).Row
    
    While 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 If
    Wend
    
    End Sub
    

提交回复
热议问题