Do While Loop

Do While... Loop works in almost exactly the same way as Do Until. Loop like except that the Until variation continues executing the body of the loop until the comparison test is true. Here is the format of the Do While... Loop structure:

Do While (comparison test)

block of one or more VBA statements Loop

The block of code continues looping as long as comparison test is true. Whether you insert one or several lines of code for the block doesn't matter. It's vital, however, that the block of code somehow changes a variable used in the comparison test. The block of code keeps repeating as long as the Do While... Loop's comparison test continues to stay true. Eventually, the comparison test must become false or your program will enter an infinite loop and the user will have to break the program's execution through an inelegant means, such as pressing the [Ctrl] + [Break] key combination.

Do While... Loop continues executing a block of VBA statements as long as the comparison test is true. As soon as comparison test becomes false, the loop terminates.

An infinite loop is a loop that never terminates. Guard against infinite loops and always make sure your loops can terminate properly. Even if you provide an Exit command button or a File > Exit menu option in your application, the program will often ignore the user's exit command if the program enters an infinite loop.

0 0

Post a comment