位置:首页 » 文章/教程分享 » VBA While Wend循环

在 While..Wend 循环,如果条件为真,所有的语句执行,直到遇到 WEND 关键字。

如果条件为假,则退出循环,控制跳到 WEND 关键字后的下一个语句。

语法:

VBA的 While..Wend 循环的语法是:

While condition(s)
   [statements 1]
   [statements 2]
   ...
   [statements n]
Wend

流程图:

示例 :

Private Sub Constant_demo_Click()
  Dim Counter :  Counter = 10   
  While Counter < 15     ' Test value of Counter.
    Counter = Counter + 1   ' Increment Counter.
    msgbox "The Current Value of the Counter is : " & Counter
  Wend   ' While loop exits if Counter Value becomes 15.
End Sub
当执行上面的代码,它打印在消息框中如下。

The Current Value of the Counter is : 11 

The Current Value of the Counter is : 12 

The Current Value of the Counter is : 13 

The Current Value of the Counter is : 14 

The Current Value of the Counter is : 15