Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 . |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
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) |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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) |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 . |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell | Excel Discussion (Misc queries) | |||
why does my "TAB" key skip a column in excel spread sheets? | Excel Worksheet Functions | |||
Question on "On Error GoTo skip" | Excel Discussion (Misc queries) | |||
How do I run a macro from inside an"=IF" formula in Excel 2003? | Excel Worksheet Functions | |||
skip checkbox to next tab with "enter" | New Users to Excel |