Reputation: 1188
The code is like:
'first for loop
for I = 1 to 5
do sth
'second for loop
for j = 2 to 7
do sth
'third for loop
for m = 2 to 43
if [condition] then
exit 2nd and 3rd loop and continue on next I ?????
end if
next
next
next
I wrote two Exit For
, but it did not help. It only exited the 3rd for loop and continue on next j.
Upvotes: 4
Views: 25843
Reputation: 21
For a = 1 To maxRows
If Exam1Grade(a) > 99 Then
For b = 1 To maxRows
If Exam2Grade(b) > 99 Then
For c = 1 To maxRows
If Exam3Grade(c) >= 100 Then
MsgBox ("Stop looking through the 3rd exam, you have just found the perfect score")
GoTo ThisPoint 'Exit All For Loops
ElseIf c = maxRows Then 'Restart At A
GoTo NextA
End If
Next c
End If
Next b
End If
NextA:
Next a
ThisPoint:
The GoTo statement can act as a break
Upvotes: 2
Reputation: 12612
Make dummy Do...Loop
inclusion:
' first for loop
For i = 1 To 5
' do sth
' dummy do loop, won't repeat, just creating a block to exit from
Do
' second for loop
For j = 2 To 7
' do sth
' third for loop
For m = 2 To 43
If [Condition] Then
' exit 2nd and 3rd loop and continue on next i
Exit Do
End If
Next
Next
Loop Until True ' never repeats
' continue within 1st for loop
Next
Upvotes: 4
Reputation: 2545
If you nest a flag in your loops you can put an if statement prior to looping on your second loop. If the flag is true, then you exit the second loop as well.
'first for loop
for I = 1 to 5
do sth
'second for loop
for j = 2 to 7
do sth
'third for loop
for m = 2 to 43
if [condition] then
flg = True
Exit for
end if
next
If flg = True then Exit For
next
next
Upvotes: 12