Skip to Next "x" inside of a For Next Loop

  • Thread starter Thread starter verizon
  • Start date Start date
V

verizon

Hello

In a large For Next loop, I have many criteria. If any one of these
criteria is a certain value I want to immediately go to the Next x. How can
I do that:

For x = 1 to 100
Criterion 1
Criterion 2
Criterion 3
Criterion 4
Next x

If, say, Criterion 2 is true, I would like to go immediately to the Next x
without considering Criteria 3 and 4.

I cannot figure out how to advance the Next x.

Thank you.

W
 
verizon said:
Hello

In a large For Next loop, I have many criteria. If any one of these
criteria is a certain value I want to immediately go to the Next x. How can
I do that:

For x = 1 to 100
Criterion 1
Criterion 2
Criterion 3
Criterion 4
Next x

If, say, Criterion 2 is true, I would like to go immediately to the Next x
without considering Criteria 3 and 4.

I cannot figure out how to advance the Next x.

For x = 1 To 100
'...
If Not (criterion1) Then
If Not (criterion2) Then
If Not (criterion3) Then
If Not (criterion4) Then
'...
End If
End If
End If
End If

'....
Next

---------------

OR

---------------

For x = 1 To 100
'...
If criterion1 Then GoTo ciao
If criterion2 Then GoTo ciao
If criterion3 Then GoTo ciao
If criterion4 Then GoTo ciao
'...
ciao:
Next
 
Doesn't an If/ElseIf or Select Case construct do this?
Both constructs end as soon as a true condition is found.

Example 1:
For x = 1 To 100
If Criteria 1 Then
'Do something
ElseIf Criteria 2 Then
'Do something
ElseIf Criteria 3 Then
'Do something
ElseIf Criteria 4 Then
'Do something
End If
Next

Example 2:
For x = 1 To 100
Select Case ActiveCell.Value
Case Is = Value1
'Do something
Case Is = Value2
'Do something
Case Is = Value3
'Do something
Case Is = Value4
'Do something
End Select
Next

Regards,
Greg
 
verizon

A couple of ways


For x = 1 To 100
Criterion 1
Criterion 2
Criterion 3
Criterion 4

If Criterion1 = False Then
'your code here
ElseIf Criterion2 = False Then
'your code here
ElseIf Criterion3 = False Then
'your code here
ElseIf Criterion4 = True Then
'your code here
ElseIf Criterion4 = False And Criterion3 = True Then
'your code here
End If
Next x




or




For x = 1 To 100
Criterion 1
Criterion 2
Criterion 3
Criterion 4

If Criterion1 = True Then
GoTo JumpHere
End If
'your code here
JumpHere:
Next
 
In a large For Next loop, I have many criteria. If any one of these
criteria is a certain value I want to immediately go to the Next x. How can
I do that:

For x = 1 to 100
Criterion 1
Criterion 2
Criterion 3
Criterion 4
Next x

If, say, Criterion 2 is true, I would like to go immediately to the Next x
without considering Criteria 3 and 4.

try this:

Sub MyLoop()
Dim x As Integer
Dim Criterion1 As Boolean
Dim Criterion2 As Boolean
Dim Criterion3 As Boolean
Dim Criterion4 As Boolean

'Criterion2 = True

For x = 1 To 100
Select Case True
Case Criterion1, Criterion2, Criterion3, Criterion4
MsgBox "someone is true"
End Select
Next x
End Sub

--
Regards
Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)
 
If, say, Criterion 2 is true, I would like to go immediately to the Next x
try this:

Sub MyLoop()
Dim x As Integer
Dim Criterion1 As Boolean
Dim Criterion2 As Boolean
Dim Criterion3 As Boolean
Dim Criterion4 As Boolean

'Criterion2 = True

For x = 1 To 100
Select Case True
Case Criterion1, Criterion2, Criterion3, Criterion4
MsgBox "someone is true"
End Select
Next x
End Sub


Supplement, if no variable is true:

Select Case True
Case Criterion1, Criterion2, Criterion3, Criterion4
MsgBox "someone is true"
Case Else
MsgBox "nothing is true"
End Select

--
Regards
Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)
 
Hi,



For x = 1 to 100
Criterion 1
Criterion 2
if criteria 2 i= true then goto nextxline 'in on word, it is just a
label
Criterion 3
Criterion 4
nextxline :
Next x
Regards,

Jean-Yves
 
This is a missing function in VB. In C++ you'd put "continue;" to restart
the loop at the next iteration. In VB this is most often done with a
If/Then/Else construct within the loop (many people have demonstrated this)
or you can "cheat" and use a GoTo, but that practice is frowned upon by most
VB programmers.
 
i think you could be right. will try.

Thx

W
Greg Wilson said:
Doesn't an If/ElseIf or Select Case construct do this?
Both constructs end as soon as a true condition is found.

Example 1:
For x = 1 To 100
If Criteria 1 Then
'Do something
ElseIf Criteria 2 Then
'Do something
ElseIf Criteria 3 Then
'Do something
ElseIf Criteria 4 Then
'Do something
End If
Next

Example 2:
For x = 1 To 100
Select Case ActiveCell.Value
Case Is = Value1
'Do something
Case Is = Value2
'Do something
Case Is = Value3
'Do something
Case Is = Value4
'Do something
End Select
Next

Regards,
Greg
 
You could also try:

For x = 1 to 100
criteria 1
criteria 2
if criteria 2 = true then goto NextX
criteria 3

NextX:
Next x

This way, the script will run normally, and if criteria2 is true i
will skip criteria 3 (and further script) and go immediately to th
Next x statement
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top