"End For" is a syntax error. Exit For will exit only the For...Next
structure that it's embedded in.
From XL/VBA Help ("Exit Statement"):
Exit For
Provides a way to exit a For loop. It can be used only in a
For...Next or For Each...Next loop. Exit For transfers control to
the statement following the Next statement. When used within nested
For loops, Exit For transfers control to the loop that is one
nested level above the loop where Exit For occurs.
Dim a(1 To 100, 1 To 1000)
Dim i As Long, j As Long
Do
For i = 1 To 100
For j = 1 To 1000
a(i, j) = Int(Rnd() * 100)
If a(i, j) = 98 Then Exit Do
Next j
Next i
Loop
Debug.Print i, j
or
Dim a(1 To 100, 1 To 1000)
Dim i As Long, j As Long
For i = 1 To 100
For j = 1 To 1000
a(i, j) = Int(Rnd() * 100)
If a(i, j) = 98 Then GoTo Continue
Next j
Next i
Continue:
Debug.Print i, j
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.