WHILE AND FOR LOOPS

  • Thread starter Thread starter paul
  • Start date Start date
P

paul

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
 
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 said:
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

Read other people's code. This ng would be a good place to start.

If you want a dictionary explanation, For loops are used when you know in
advance how many iterations you want to perform. [Do] While (and Do Until)
loops are used when you know the continuation (or stopping) criteria but not
necessarily the number of iterations needed.
 
Back
Top