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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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>
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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