View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default WHILE AND FOR LOOPS

A For and a While loop is used to repeat an action a number of times,
changing the object being acted upon, or until a set number of actions has
been performed.

For instance, you could select a range of cells in Excel, then this code

For each cell in Selection
cell.Interior.ColorIndex = 3
Next cell

will set the cell colour of each cell to 3.

You can also use an index counter in a loop, like so

For i = 1 To Selection.Rows.Count Step 2
Selection.Cells(i, 1).Interior.ColorIndex = 3
Next i

colours very other row of the first column.

While loops are similar, where you test while a condition is true, but are
not used as much.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"paul" wrote in message
ps.com...
Can someone explain what a While loop and a For loop is and
demonstrate an example ogf how it could be used in VBA. As i am
struggling to understand the concept and example of it in use

please

thanks