Do vs. For

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hey guys,

As some of you have already seen from my posts 'helping' people my VBA knowledge is not brilliant, but at least I try ;-)

I have always wondered why you would ever need a Do loop if a For does everything and more.

Some languages don't even include Do loops.

Cheers,

James
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How would you do a FindNext loop using For rather than Do?
 
Not sure what you mean. I thought For... Next was similar?
 
Not always. For next is fine if you have a specific number of iterations to go through, or you want to loop through every item in a collection. If you do a Find and FindNext, you don't know how many iterations there will be (if any) so you just want to keep going until either there are no more found items or you are back at the start. Do...Loop is perfect for that. Why there's a While...Wend as well, I have no idea!
 
I use For..Next when I have a finite number of iterations that will never change. Do..Until or Do..While all other times. I never use While..Wend--it just seems pointless when Do..While is available.
 
but can't this be done in a for loop? Something like:

For x to end of file?
 
If you want to search cell by cell, then yes you could use For...Next. But using Find and FindNext, I can't think of a way offhand.
 
I think.....

Code:
set rng = .Find(.....)
If not rng is nothing then
    For i = 1 To Worksheetfunction.Countif(range,find value) - 1
         set rng = .FindNext(rng)
    Next i
end if

or something similar....
 
but can't this be done in a for loop? Something like:

For x to end of file?

The Do While | Do Until... structures also allow cleaner coding from an "end when this is (or is no longer) true" perspective whereas in a For/Next loop you would be obliged to use an If...Then Exit For test and jump. The Do structures also are nice in that you can deliberately set them up to "under the right conditions, never run"; i.e. the test is in the Do portion. Or you can go with a "no matter what, always run at least once"; i.e. the test is in the Loop portion. Yes, you could probably encase the FOR/NEXT inside an IF in most cases. Likewise, you can use IF,ELSEIF,ELSEIF...ENDIF instead of SELECT CASE statements in most cases. In general, I would opt for SELECT CASE, all other things being equal.

As for Tina & While...Wend, I'll toss one in the mix now and again just for variety. :-D
 
Last edited:

Forum statistics

Threads
1,222,630
Messages
6,167,191
Members
452,104
Latest member
jadethejade

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top