Info

At the end of the loop, you get the result shown in Figure 5.7.

Figure 5.7

After the loop completes all 10 iterations, this is the result.

Figure 5.7

After the loop completes all 10 iterations, this is the result.

The common use for such a loop is to walk through all the rows in a dataset and decide to perform some action on the basis of a criteria. For example, if you want to mark all the rows with positive service revenue in Column F, you could use this loop:

Cells(I, 8).value = "Service Revenue" Cells(I, l).resize(1, 8).Interior.ColorIndex = 4 End If Next i

This loop checks each item of data from Row 2 through Row 10. If there is a positive number in Column F, then Column H of that row will have a new label and the cells in Columns A:H of the row will be colored green. After running this macro, the results look like Figure 5.8.

Figure 5.8

After the loop completes all 9 iterations, any rows with positive values in Column F are colored green and have the label "Service Revenue"added to Column H.

Figure 5.8

After the loop completes all 9 iterations, any rows with positive values in Column F are colored green and have the label "Service Revenue"added to Column H.

0 0

Post a comment