VBA - While Wend 循环



While...Wend 循环中,如果条件为真,则将一直执行所有语句,直到遇到关键词 Wend

如果条件为假,则退出循环,并且控件会跳转到关键词 Wend 后面的下一个语句。

语法

下面是 VBA 中 While..Wend 循环的语法。

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

流程图

While Loop Architecture

示例

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
vba_loops.htm
广告