while wend loop vba

In a While wend loop, if the condition is True, all the code block are executed until the condition is true. If the condition is false, the loop is exited and the control jumps to the very next statement when condition is false. in while loop we manually increment the counter so that we can check condition in next loop

Syntax

Following is the syntax of a While..Wend loop in VBA.

While condition(s)
   [statements 1]
   [statements 2]
   [statements n]
   s= s+1
wend

Example

Sub whileloop_ex()

   Dim C as integer
   C = 0   
  
   While Counter < 10     ' Test value of Counter.
    
 debug.print "The Current Value of the Counter is : " & C

 Counter = Counter + 1   ' Increment Counter.
 ‘While loop exits if Counter Value becomes 10.
   wend
End Sub  



Result

The Current Value of the Counter is : 1

The Current Value of the Counter is : 2

The Current Value of the Counter is : 3

The Current Value of the Counter is : 4

The Current Value of the Counter is : 5

The Current Value of the Counter is : 6

The Current Value of the Counter is : 7

The Current Value of the Counter is : 8

The Current Value of the Counter is : 9


Post a Comment

0 Comments