Sub ContinueTest()
Dim i As Integer
'Outputs 1 & 10
For i = 1 To 10: Do
If i > 1 And i < 10 Then
'Continue the for loop
Exit Do
End If
Debug.Print i
'Never let the DO execute more than once
Exit Do: Loop
'Perform any actions here that must be done at the end of
'every iteration, whether or not you "continued".
Next
End Sub
To be honest I can't see any benefit in that. Care to elaborate?
'Totally fictional example: an outer and inner loop, each going from 1 to 10
'When outer is odd, we only want to output inner 1-5; when outer is even, only inner 6-10
'We also want to prevent any output when outer is 3 or inner is 6
Sub ExampleWithContinues()
For outer = 1 To 10: Do
If outer = 3 Then Exit Do
For inner = 1 To 10: Do
If inner = 6 Then Exit Do
If outer Mod 2 = 1 And inner > 5 Then Exit Do
If outer Mod 2 = 0 And inner <= 5 Then Exit Do
Debug.Print "Outer: ", outer, "Inner:", inner
Exit Do: Loop: Next
Exit Do: Loop: Next
End Sub
Sub ExampleWithIfs()
For outer = 1 To 10
If outer <> 3 Then
For inner = 1 To 10
If inner <> 6 Then
If (outer Mod 2 = 1 And inner <= 5) Or _
(outer Mod 2 = 0 And inner > 5) Then
Debug.Print "Outer: ", outer, "Inner:", inner
End If
End If 'inner <> 6
Next
End If 'outer is <> 3
Next
End Sub
If outer = 2 and inner = 8 Then Exit Do
For X = Whatever To WhateverElse
....
.... <<< code >>>
....
If SomeCondition Then GoTo Continue
....
.... <<< more code >>>
....
Continue:
Next
This thread is too long to read, so I don't know if this was covered earlier or not, but this is how you would implement the equivalent of Continue in VB/VBA...
Code:For X = Whatever To WhateverElse .... .... <<< code >>> .... If SomeCondition Then GoTo Continue .... .... <<< more code >>> .... Continue: Next
For X = Whatever To WhateverElse: Do
....
.... <<< code >>>
....
If SomeCondition Then Exit Do
If SomeOtherCondition Then Exit Do
....
.... <<< more code >>>
....
Exit Do: Loop: Next
First off, I did not plan on having "several arbitrarily-named line labels" in my code... just the one Continue label to jump to for all conditions needing to do the Continue operation. Second, I am not so sure I like your Do..Loop proposal. As you have structured it, the only way for the For loop to iterate is for some tested condition to be True so that an Exit Do statement gets executed. Not all loops that implement a Continue anticipate it being used... it is possible for a For..Next loop to iterate completely without ever executing a Continue statement depending on the conditions being examined within the loop... my code allows for that structure and I do not believe that your code does.I agree, Rick, that is a perfectly viable way to do it, and I have certainly used Gotos to great effect. However, if, for whatever reason, you had to do this multiple times in one routine, this alternative option may end up a little cleaner than having several arbitrarily-named line labels:
Code:For X = Whatever To WhateverElse: Do .... .... <<< code >>> .... If SomeCondition Then Exit Do If SomeOtherCondition Then Exit Do .... .... <<< more code >>> .... Exit Do: Loop: Next
First off, I did not plan on having "several arbitrarily-named line labels" in my code... just the one Continue label to jump to for all conditions needing to do the Continue operation. Second, I am not so sure I like your Do..Loop proposal. As you have structured it, the only way for the For loop to iterate is for some tested condition to be True so that an Exit Do statement gets executed. Not all loops that implement a Continue anticipate it being used... it is possible for a For..Next loop to iterate completely without ever executing a Continue statement depending on the conditions being examined within the loop... my code allows for that structure and I do not believe that your code does.
For X = Whatever To WhateverElse
Do
<<< code >>>
If SomeCondition Then Exit Do 'Continue to the Next statement
If SomeOtherCondition Then Exit Do 'Continue to the Next statement
<<< more code >>>
Exit Do 'Ensures the loop will never execute more than once
Loop
Next
This thread is too long to read, so I don't know if this was covered earlier or not, but this is how you would implement the equivalent of Continue in VB/VBA...
Code:For X = Whatever To WhateverElse .... .... <<< code >>> .... If SomeCondition Then GoTo Continue .... .... <<< more code >>> .... Continue: Next