Info

For...Next Loops 103

Do Loops 108

The VBA Loop: For Each 113

Case Study 115

Next Steps 116

.Next

For...Next Loops

These are common loop constructs. Everything between For and Next is run multiple times. Each time that the code runs, a certain counter variable (specified in the For statement) has a different value.

Consider this code:

As this program starts to run, we've given the counter variable a name of I. The first time through the code, the variable I is set to 1. The first time that the loop is executed, I is equal to 1 so the cell in Row 1, Column 1 will be set to 1 (see Figure 5.1).

Figure 5.1

After the first iteration through the loop,the cell in Row 1,Column 1 has the value of 1.

0 0

Post a comment