Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default WHILE AND FOR LOOPS

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,231
Default WHILE AND FOR LOOPS

"paul" wrote...
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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops & Ifs Shorty[_3_] Excel Programming 4 March 4th 06 11:06 AM
Loops David Excel Programming 1 February 10th 06 10:55 PM
for each loops adncmm1980[_3_] Excel Programming 1 October 4th 04 12:56 PM
LOOPS Fernando Duran Excel Programming 2 September 3rd 03 01:07 AM
Loops Tom Ogilvy Excel Programming 0 July 18th 03 05:20 PM


All times are GMT +1. The time now is 01:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"