An Interactive Learning Tool

Proud to be a Teacher


Loop

In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number. If it hasn't, the next instruction in the sequence is an instruction to return to the first instruction in the sequence and repeat the sequence. If the condition has been reached, the next instruction "falls through" to the next sequential instruction or branches outside the loop. A loop is a fundamental programming idea that is commonly used in writing programs.
An infinite loop is one that lacks a functioning exit routine . The result is that the loop repeats continually until the operating system senses it and terminates the program with an error or until some other event occurs (such as having the program automatically terminate after a certain duration of time).
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.

Do....While Loop

A Do…While loop is used when we want to repeat a set of statements as long as the condition is true. The condition may be checked at the beginning of the loop or at the end of the loop.

Syntax

Following is the syntax of a Do…While loop in VBA.
Do While condition
    [statement 1]
Loop

Example

The following example uses Do…while loop to check the condition at the beginning of the loop. The statements inside the loop are executed, only if the condition becomes True.

Above example will prompt message box with value of iVal and loop until it appears iVal at 10.

While...Wend

Example

This example uses the While...Wend statement to increment a counter variable. The statements in the loop are executed as long as the condition evaluates to True. Dim Counter
Counter = 0 ' Initialize variable
While Counter < 20 ' Test value of Counter
Counter = Counter + 1 ' Increment Counter
Wend ' End While loop when Counter > 19
TIP:

The Do...Loop statement provides a more structured and flexible way to perform looping

For..Next Loop

A For...Next loop is a repetition control structure that allows a developer to efficiently write a loop that needs to be executed a specific number of times.

Syntax

For counter = start To end [Step stepcount]
    [statement 1]
Next

Examples


Each time msgbox appear value next to previous until reaches to 10 by adding 2 in each next step

For...Each

A For Each loop is used to execute a statement or a group of statements for each element in an array or collection.
A For Each loop is similar to For Loop; however, the loop is executed for each element in an array or group. Hence, the step counter won't exist in this type of loop. It is mostly used with arrays or used in context of the File system objects in order to operate recursively.

Syntax

Following is the syntax of a For Each loop in VBA.

For Each element In Group
    [statement 1]
Next

Example


When the above code is executed, it prints all the fruit names with one item in each line



Terms of use Website Policy Contact us
© Copyrights ExcelYourVBA.com 2020