loops????

H

harry buggy

can nay one help as 2 what a while and for a for loop does in vba?

and what are the syntaxes for these?

also could any1 demonstrate how this would work in a section of code
becasue there are so many types of loops and im gettinmg confused

please help
 
G

Guest

There are basically 3
A Do While does while something is true as in
Do While myValue=True
stuff
Loop
it will do the code (stuff) until something changes myValue to False

There is a Do Until which is kind of the opposite
Do Until myValue=False
stuff
Loop
This is the one I use most often, do something until cells are blank is most
common

and For Next is for when you know how many times to do something
For i = 1 To sheets.count
stuff
Next
This is one used often as well, the one above sets a base of 1 and does
something for however many sheets there are. Handy because you can step
forward backward 2 at a time etc.

All really depends on your situation but you can almost always pick any of
the 3 and make it suit your needs.
 
R

Rick Rothstein \(MVP - VB\)

Just for completeness sake, there are two other loop formats..

Do
Stuff
Loop While <<logical test>>

and

Do
Stuff
Loop Until <<logical test>>

The difference between these forms and their Do While/Until counterparts is
that the Loop While/Until versions will always execute the Stuff statements
at least one time because the exit condition isn't tested until the end of
the loop.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top