Excel VBA - sortir d'une boucle For

Comment sortir prématurément d’une boucle For en Excel VBA ?

Utilisez l’instruction Exit For :

For i = 1 To 100
    If Cells(i, 1).Value = "stop" Then
        Exit For
    End If
Next i

Pour les autres types de boucles :

  • Exit Do pour les boucles Do…Loop
  • Exit While pour les boucles While…Wend (n’existe pas — utilisez une variable booléenne ou convertissez en Do While)
  • Exit Sub pour quitter complètement la procédure
  • Exit Function pour quitter une fonction