Only Run part of a DO loop on first instance

mantis_sg

Board Regular
Joined
Nov 16, 2005
Messages
126
Is the there a way to skip some code after the first loop? e.g.

I have a do loop that loops 5 times, I want all the code to be executed during the first loop but for the 2nd,3rd,4th and 5th times I only want a certain ammount of the code to run...?????
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
Simplest is to just create a boolean switch and set to TRUE at the end of the loop. Place the conditional code in an If/EndIf statement and you're done.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> FooFoo()
    <SPAN style="color:#00007F">Dim</SPAN> booDoneOnce <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>
    
    booDoneOnce = <SPAN style="color:#00007F">False</SPAN>         <SPAN style="color:#007F00">' optional</SPAN>
    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> SomeCondition
        <SPAN style="color:#007F00">' code</SPAN>
        <SPAN style="color:#007F00">' code</SPAN>
        <SPAN style="color:#007F00">' more very interesting stuff</SPAN>
        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> booDoneOnce <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#007F00">' code that should</SPAN>
            <SPAN style="color:#007F00">' only execute the one time</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        booDoneOnce = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">Loop</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Public Sub test()
Dim i As Integer
For i = 1 To 5
If i = 1 Then
MsgBox "i= " & i & " here is where you put the only = 1 stuff"
End If
MsgBox "i = " & i & " Here is where you put the i>1 or i =1 stuff"
Next i
End Sub

Gene, "The Mortgage Man", Klein
 

mantis_sg

Board Regular
Joined
Nov 16, 2005
Messages
126

ADVERTISEMENT

Thanks a lot for your help, the number of times the loop is run is random so the last suggestion wouldn't work, I will try the otheres, thanks anayway.
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Thanks a lot for your help, the number of times the loop is run is random so the last suggestion wouldn't work, I will try the otheres, thanks anayway

I assume your loop will still look something like:
.....
lower=whatever
upper=YourRandomFunction()
For i=lower to upper
code stuff
Next i

In fact I can't imagine how else you would do a for..next loop - I mean the upper has to be defined in one of two ways - hardcoded (like I did) or by previous code. Either way it has to be something in order for it to be there.

Gene, "The Mortgage Man", Klein
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022

ADVERTISEMENT

Gene, note the title of the post -- ...part of a DO loop...; not ...part of a FOR loop...
 

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Well at least I got the letter "o" right. :oops:

Gene, "The Mortgage Man", Klein
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
Hi Jon,

Certainly one possible interpretation. But I interpreted this to mean between iterations of the loop itself and not between successive calls to the loop or the procedure within which it resides; which would be where I could see use of a static coming into play.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,809
Messages
5,833,790
Members
430,233
Latest member
cbutts

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
Top