VBA - While Wend Loops

Em um While…Wend loop, se a condição for True, todas as instruções são executadas até o Wend palavra-chave for encontrada.

Se a condição for falsa, o loop é encerrado e o controle pula para a próxima instrução após o Wend palavra-chave.

Sintaxe

A seguir está a sintaxe de um While..Wend loop em VBA.

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

Diagrama de fluxo

Exemplo

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

Quando o código acima é executado, ele imprime o seguinte em uma caixa de mensagem.

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