Inner and Outer Loops , Exiting a Loop , Exit Do

Inner and Outer Loops

Any type of Loop can have more than one level. This is very similar to Nesting Worksheet formulas on a Worksheet. There is no limit (except memory) of the level to which you can Nest loops. To keep things simple though we will only look at a two level Loop. Let’s say we want to Loop through all cells in the Range A1:A10 on each Worksheet and place the address of the cell in each cell. To do this we would use:

Exiting a Loop

In all the above Loop examples we have allowed the loop to continue on until the loop condition is met. But there are times when we may wish to force our loop to leave a loop early. This is done by using the Exit Statement. Let’s assume we wish to loop through a range of cells and select a cell if it’s value is 100.

Exit Do

So that is basically all there is to Loops. Used in the context as shown above would not really be of much use, but it is far more important to understand the concept of them than to use them without knowing how they work. The only other part of Loops that you will most likely encounter is what is known as the endless Loop. This occurs when you start a loop that will never meet the condition you have set and so it just keeps going around endlessly. When this happens you need to push Ctrl+Break or Esc.