ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Skip to Next "x" inside of a For Next Loop (https://www.excelbanter.com/excel-programming/297743-skip-next-x-inside-next-loop.html)

verizon

Skip to Next "x" inside of a For Next Loop
 
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



cucchiaino

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

"verizon" ha scritto nel messaggio
...
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



Greg Wilson[_4_]

Skip to Next "x" inside of a For Next Loop
 
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

-----Original Message-----
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


.


mudraker[_211_]

Skip to Next "x" inside of a For Next Loop
 
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
JumpHe
Next

--
Message posted from http://www.ExcelForum.com


Melanie Breden

Skip to Next "x" inside of a For Next Loop
 
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)


Melanie Breden

Skip to Next "x" inside of a For Next Loop
 
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



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)


Jean-Yves[_2_]

Skip to Next "x" inside of a For Next Loop
 
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



"verizon" wrote in message
...
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





JT Lovell

Skip to Next "x" inside of a For Next Loop
 
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.

--
JT Lovell

"verizon" wrote in message
...
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

Skip to Next "x" inside of a For Next Loop
 
i think you could be right. will try.

Thx

W
"Greg Wilson" wrote in message
...
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

-----Original Message-----
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


.




Berend Botje[_10_]

Skip to Next "x" inside of a For Next Loop
 
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

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 02:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com