位置:首頁 > 高級語言 > VBA教學 > VBA While Wend循環

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