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
廣告
© . All rights reserved.