My Simple Loop

My Simple Loop

There are a number of ways of writing loops but I like the Do Until way of doing things. The basic structure is as follows:
Do Until 'whatever criteria you set, see below'
'code for the action you want'
Loop

This will repeat the loop until the criteria has been met. A simple use would be to go down a list of data until it finds a cell with a certain value. Each time the Loop runs it will select the next cell down using the Offset command, until the selected cell contains ‘ABC’.
Range("A1").Select
Do Until Activecell.Value="ABC"
ActiveCell.Offset(1,0).Select
Loop

You could also use a counter to run the Loop a set number of times. Each time the Loop runs it will update the Counter Variable until it reaches 3.
Counter=1
Do Until Counter=3
'code for the action you want'
Counter=Counter+1
Loop

No Comments

Post a Comment