VBA While Wend迴圈

2019-10-16 23:14:02

While...Wend迴圈中,如果條件為True,則會執行所有語句,直到遇到Wend關鍵字。

如果條件為false,則退出迴圈,然後控制元件跳轉到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