VBA_How 中途结束一个For循环,进行下一个
VBA_How to end a For cycle in halfway and carry out the next one
早上好,
我正在 EXCEL 处理质量控制报告。
我有两个table,一个是我的table,另一个是原来的table。我想浏览原始文件并搜索关键信息并将其放入我的报告 table 中的正确单元格中。
这是我的想法:
For each line i in my table
for each line j in the report table
if it contains the main title and the correct zone
for each cell k in this line j
if there is my answer(like coulour:red)
put it in my table
[HERE I WANT TO QUIT THE FOR CYCLE OF k AND j AND GOTO NEXT i]
end if
next k
end if
next j
next i
我尝试了一些方法,例如添加“下一个 j”但失败了。它会导致错误。
所以如果你有什么想法请告诉我。
有几种方法可以做到这一点。如果您需要完全退出所有循环,我知道的唯一方法是使用 do while
循环来递增循环内的 i 和 j 值。然后,当您达到所需的条件时,将值加一并使用 GoTo 语句将代码发送回循环的开头。
然而,由于您似乎只想退出 K 和 J 循环而留在 I 中,因此请使用 "Exit For" 语句。我认为您可能必须以某种方式重复搜索条件以在退出第一个循环后退出第二个循环,但是使用由第一个退出语句触发的布尔值将使这很容易
你可以试试这个算法:
declare exit flag for j and set it to false
For each line i in my table
for each line j in the report table
if it contains the main title and the correct zone
for each cell k in this line j
if there is my answer(like coulour:red)
put it in my table
set exit flag for j to true '<-- update
exit for '<-- update
end if
next k
end if
if exit flag for j is true then set it to false and exit for '<-- update
next j
next i
早上好,
我正在 EXCEL 处理质量控制报告。 我有两个table,一个是我的table,另一个是原来的table。我想浏览原始文件并搜索关键信息并将其放入我的报告 table 中的正确单元格中。 这是我的想法:
For each line i in my table
for each line j in the report table
if it contains the main title and the correct zone
for each cell k in this line j
if there is my answer(like coulour:red)
put it in my table
[HERE I WANT TO QUIT THE FOR CYCLE OF k AND j AND GOTO NEXT i]
end if
next k
end if
next j
next i
我尝试了一些方法,例如添加“下一个 j”但失败了。它会导致错误。 所以如果你有什么想法请告诉我。
有几种方法可以做到这一点。如果您需要完全退出所有循环,我知道的唯一方法是使用 do while
循环来递增循环内的 i 和 j 值。然后,当您达到所需的条件时,将值加一并使用 GoTo 语句将代码发送回循环的开头。
然而,由于您似乎只想退出 K 和 J 循环而留在 I 中,因此请使用 "Exit For" 语句。我认为您可能必须以某种方式重复搜索条件以在退出第一个循环后退出第二个循环,但是使用由第一个退出语句触发的布尔值将使这很容易
你可以试试这个算法:
declare exit flag for j and set it to false
For each line i in my table
for each line j in the report table
if it contains the main title and the correct zone
for each cell k in this line j
if there is my answer(like coulour:red)
put it in my table
set exit flag for j to true '<-- update
exit for '<-- update
end if
next k
end if
if exit flag for j is true then set it to false and exit for '<-- update
next j
next i