如何在ExcelVBA中退出超过1个for循环?
How to exit more than 1 for loop in Excel VBA?
代码如下:
'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
我写了两个Exit For
,但没有用。它只退出第 3 个 for 循环并在下一个 j 继续。
如果在循环中嵌套标志,则可以在循环第二个循环之前放置一个 if 语句。如果标志为真,那么你也退出第二个循环。
'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
制作虚拟Do...Loop
包含:
' 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
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:
GoTo 语句可以作为中断
代码如下:
'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
我写了两个Exit For
,但没有用。它只退出第 3 个 for 循环并在下一个 j 继续。
如果在循环中嵌套标志,则可以在循环第二个循环之前放置一个 if 语句。如果标志为真,那么你也退出第二个循环。
'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
制作虚拟Do...Loop
包含:
' 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
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:
GoTo 语句可以作为中断