Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loops

Status
Not open for further replies.

venkman

Programmer
Oct 9, 2001
467
US
What's the equivalent of c++'s "continue" keyword in VBA?
In other words how do you cause a loop to restart. For instance, given this code:

dim i as integer
i = 0
do
i = i + 1
msgbox "i is " & i
if i > 5 then
CONTINUE
end if
i = i + 1
loop until i > 10

if CONTINUE is the statement I'm looking for, then this is the output of the previous code:
i is 1
i is 3
i is 5
i is 7
i is 8
i is 9
i is 10

-Venkman
 
I'm guessing you could use
DO WHILE i<10
'code here
i=i+1
LOOP Rgds
Geoff

Vah! Denuone Latine loquebar? Me ineptum. Interdum modo elabitur
 
Sorry Geoff, but I'm extremely confused by your answer. I'm looking for a continue statement. I'm not sure what you meant by your last response. Can you please elaborate?

Thanks,
Venkman
 
venkman

Sub tloop()
Dim i As Integer
i = 0
Do Until i = 10
i = i + 1
MsgBox &quot;i is &quot; & i
If i < 5 Then
i = i + 1
End If

Loop
End Sub


This is how a a DO UNTIL statement would fit into your code,
Geoffs' DO WHILE would simply require a of the line

Do Until i = 10
to
Do While i<10

Im not familiar with C++ so I am assuming that the continue statement would break the loop and restart it again at the point marked with the continue statement. Is this correct?
I am not aware that this is possible with a single command in VBA so you'll have to try a work around. But saying that guys like geoff, here are far more experinced in VBA and will point us both in the right direction if Im wrong.




 
Why not just condition the code following the &quot;CONTINUE&quot; with the inverse logic? Here is a way in VBA:
Code:
dim i as integer
i = 0
do
  i = i + 1
  msgbox &quot;i is &quot; & i
  if i <= 5 then
    i = i + 1
  end if
loop until i > 10
I never liked the CONTINUE statement anyway.
 
Zathras, I was just using the code as an example. The actual code has more complex branching using if-then-else statements, and would be hard to follow if I tried to apply your rewrite to it.

Andrew, your assessment of what a continue statement is is absolutely correct. I had thought there was an equivalent in VBA, but I guess I was wrong. In the meantime, the solution I've been using involves placing a label before the loop and using a goto. Gotos can be a little unclear when re-reading the code, so I usually try to avoid them, but if that is the only good solution than that is what I'll use.

Thanks all for your help.

-Venkman
 
Hi!

The continue statement is just a specialized form of the Go To. You can handle what you are trying to do with the Go To statement. Naming your label appropriately should keep the code readable. Note: Using conditional statements is usually more 'correct' coding procedure. It must be admitted that sometimes nested if statements can make for very ugly code and often difficult to maintain. If this is the case for your code it is possible that you could re-write it using Select Case statements and/or procedures/functions to provide readable code that also follows all of the best programming practices.

Just a thought.
hth
Jeff Bridgham
bridgham@purdue.edu
 

Hi All,

I don't know C++ so am not exactly sure what Continue does in different circumstances. In the Do loop here it is possible to replace it with a GoTo but I would prefer to avoid it if at all possible. I often find a need to iterate a For-Next loop from some point within and this is harder to accommodate with a backward GoTo. I have simply had to adopt a coding style that suits the language and take a bit longer to think about my code - it usually ends up better for it.

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top