📜  VBA-循环

📅  最后修改于: 2020-11-19 09:11:29             🧑  作者: Mango


在某些情况下,您需要多次执行一个代码块。通常,语句是按顺序执行的:函数的第一个语句首先执行,然后第二个执行,依此类推。

编程语言提供了各种控制结构,允许更复杂的执行路径。

循环语句使我们可以多次执行一个语句或一组语句。以下是VBA中循环语句的一般形式。

循环架构

VBA提供以下类型的循环来处理循环需求。单击以下链接以查看其详细信息。

Sr.No. Loop Type & Description
1 for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

2 for ..each loop

This is executed if there is at least one element in the group and reiterated for each element in a group.

3 while..wend loop

This tests the condition before executing the loop body.

4 do..while loops

The do..While statements will be executed as long as the condition is True.(i.e.,) The Loop should be repeated till the condition is False.

5 do..until loops

The do..Until statements will be executed as long as the condition is False.(i.e.,) The Loop should be repeated till the condition is True.

循环控制语句

循环控制语句从其正常顺序更改执行。当执行离开作用域时,将不执行循环中的所有其余语句。

VBA支持以下控制语句。单击以下链接以查看其详细信息。

S.No. Control Statement & Description
1 Exit For statement

Terminates the For loop statement and transfers the execution to the statement immediately following the loop

2 Exit Do statement

Terminates the Do While statement and transfers the execution to the statement immediately following the loop