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...?????
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
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,014

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,014
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,539
Messages
5,572,786
Members
412,484
Latest member
deezina07
Top