VBA question

  • Thread starter Thread starter Robert Nguyen
  • Start date Start date
R

Robert Nguyen

Could somebody please tell me the advantages and
disadvantages of each loop statement (do until, for each,
case, etc.)?

The only one I'm most comfortable with is do until with if
statement. I use it for almost everything and it works
just fine.
 
I didn't mean to answer my own question, if that's what
you're implying.

I want to know because I'm wondering if my codes could be
shorter and more efficient.
 
Robert,

Do Until loops/ While loops
are good for when you don't know exactly how many iterations you will need.

For Each are good for looping through collections or objects:
For Each myCell In Range("A1:A10")

For To are good for when you know exactly how many you want:
For i = 1 To 11

Case Select is good for when you have only one out of many possibilities,
with each action different.

HTH,
Bernie
MS Excel MVP
 
I was just saying that a chevy is a chevrolet. Use what you like best. AFAIK
performance is the same.
Most seem to use for/each most of the time.
 
Bernie,

Since I learn everything by trial and error, I don't know
the basic concept of each one. This is exactly what I
need. Thank you.
 
Back
Top