All of these examples are "quick and dirty" loops. They are great where you need to accomplish a task quickly. The Do...Loop provides a number of options to allow you to automatically have the program stop when it accomplishes the end of the task.

The first option is to have a line in the Do...Loop that detects the end of the dataset and exits the loop. In the current example, this could be accomplished by using the Exit Do command in an If statement. If the current cell is on a cell that is empty, you can assume that you've reached the end of the data and stopped processing the loop:

If Not Selection.Value > "" then Exit Do Selection.Copy

ActiveCell.Offset(0, 1).Range("A1").Select ActiveSheet.Paste

ActiveCell.Offset(1, -l).Range("A1").Select Application.CutCopyMode = False Selection.Copy

ActiveCell.Offset(-1, 2).Range("A1").Select ActiveSheet.Paste

ActiveCell.Offset(2, -2).Range("A1").Select Application.CutCopyMode = False Selection.Copy

ActiveCell.Offset(-2, 3).Range("A1").Select ActiveSheet.Paste

ActiveCell.Offset(4, -3).Range("A1").Select

Loop End Sub

0 0

Post a comment