[英]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
,但没有帮助。 它只退出第三个 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 语句可以作为中断
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.