Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Hi,
I have a for next looping in vba that had many if and Do Until in it, but not embedded. I'd like to say to the loop "Next i" in a If condition if this If condition in the beginning in the loop is respected. Is there a way to do so? Thx ! JJD -- Message posted via http://www.officekb.com |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Hi. If I understand it correctly, is this something that would work?
Sub Demo() Dim J As Long For J = 1 To 10 ' do something If J 2 Then Exit For Next J End Sub HTH. :) -- Dana DeLouis Win XP & Office 2003 "Jean-Jerome Doucet via OfficeKB.com" wrote in message ... Hi, I have a for next looping in vba that had many if and Do Until in it, but not embedded. I'd like to say to the loop "Next i" in a If condition if this If condition in the beginning in the loop is respected. Is there a way to do so? Thx ! JJD -- Message posted via http://www.officekb.com |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Not exactly but you understand the way I think. I want to say Next and not
exit Sub Demo() Dim J As Long For J = 1 To 10 ' do something If J 2 Then Exit For ' it would be there an alternative Next J then it skips all the rest of the code in this loop J and go to the next J Do Unit 'A lot of do something after the If but not related to the If and not embedded with it. Next J End Sub Dana DeLouis wrote: Hi. If I understand it correctly, is this something that would work? Sub Demo() Dim J As Long For J = 1 To 10 ' do something If J 2 Then Exit For Next J End Sub HTH. :) Hi, [quoted text clipped - 8 lines] JJD -- Message posted via http://www.officekb.com |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Sub Demo()
Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell End if Next J End Sub -- Regards, Tom Ogilvy "Jean-Jerome Doucet via OfficeKB.com" wrote in message ... Not exactly but you understand the way I think. I want to say Next and not exit Sub Demo() Dim J As Long For J = 1 To 10 ' do something If J 2 Then Exit For ' it would be there an alternative Next J then it skips all the rest of the code in this loop J and go to the next J Do Unit 'A lot of do something after the If but not related to the If and not embedded with it. Next J End Sub Dana DeLouis wrote: Hi. If I understand it correctly, is this something that would work? Sub Demo() Dim J As Long For J = 1 To 10 ' do something If J 2 Then Exit For Next J End Sub HTH. :) Hi, [quoted text clipped - 8 lines] JJD -- Message posted via http://www.officekb.com |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Tom, I don't see how it solvesmy problem. It is a regular for next you
presented me here. I want to put a line code that specify in another place (in the if) to do Next J with arriving to the official Next J. It the same principle as Exit For, excepted that I don't want to exit but to go to the next J. Tom Ogilvy wrote: Sub Demo() Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell End if Next J End Sub Not exactly but you understand the way I think. I want to say Next and not exit [quoted text clipped - 28 lines] JJD -- Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.a...mming/200507/1 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
That is exactly what I gave you.
If you want something more obscene Sub Demo() Dim J As Long For J = 1 To 10 ' do something If Cells(1, J).Value 2 Then GoTo Line3 ' code to process the cell Debug.Print J Line3: Next J End Sub Welcome to the 70's -- Regards, Tom Ogilvy "Jean-Jerome Doucet via OfficeKB.com" wrote in message ... Tom, I don't see how it solvesmy problem. It is a regular for next you presented me here. I want to put a line code that specify in another place (in the if) to do Next J with arriving to the official Next J. It the same principle as Exit For, excepted that I don't want to exit but to go to the next J. Tom Ogilvy wrote: Sub Demo() Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell End if Next J End Sub Not exactly but you understand the way I think. I want to say Next and not exit [quoted text clipped - 28 lines] JJD -- Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.a...mming/200507/1 |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
What you are asking to do is have another Next statement within the If block.
No Can Do. For-Next statements must be balanced, and within the same nested level. Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell Next J <---------- you want to put another Next statement here? End if Next J Nope. Even if it did work it would be poor program structure. Try reorganizing your If-Then-Else blocks, or use the Select statement so that whatever is performed in the desired block of code exits just before the Next statement. "Jean-Jerome Doucet via OfficeKB.com" wrote: Tom, I don't see how it solvesmy problem. It is a regular for next you presented me here. I want to put a line code that specify in another place (in the if) to do Next J with arriving to the official Next J. It the same principle as Exit For, excepted that I don't want to exit but to go to the next J. Tom Ogilvy wrote: Sub Demo() Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell End if Next J End Sub Not exactly but you understand the way I think. I want to say Next and not exit [quoted text clipped - 28 lines] JJD -- Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.a...mming/200507/1 |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Next For looping in a If
Well, I have found a way yo bypass the problem. And I think it will probably
be a cleaner solution as trying to put another Next J as you say Charlie ;) I embedded all the rest of the code after the If with a Do Until and If the condition If is fullfilled at the begginning a boolean variable becomes False. So it says Do Until boolean variable is False. As it is already False it will skip all this part of the code. Tom, I found your code most interesting though, the one with the instruction to go to Line3. Thanks to both of you for your help. I'll include the logic I've used finally at the end of this message. Regards, JJD Sub Demo() Dim J As Long For J = 1 To 10 ' do something If variable = "N" Then ("N" for new) Bool = False Else Bool= True End If Do Until Bool = False 'Here is all the rest of the Do and If conditions of the Next For loop. Bool = False 'My boolean value becomes False if it was True at the beginning and that the all the embedded code in this loop is used. It will therefor not repeat itself again. Loop Next J End Sub Charlie wrote: What you are asking to do is have another Next statement within the If block. No Can Do. For-Next statements must be balanced, and within the same nested level. Dim J As Long For J = 1 To 10 ' do something If Cells(1,J).Value 2 then ' code to process the cell Next J <---------- you want to put another Next statement here? End if Next J Nope. Even if it did work it would be poor program structure. Try reorganizing your If-Then-Else blocks, or use the Select statement so that whatever is performed in the desired block of code exits just before the Next statement. Tom, I don't see how it solvesmy problem. It is a regular for next you presented me here. I want to put a line code that specify in another place [quoted text clipped - 17 lines] JJD -- Message posted via http://www.officekb.com |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Looping | Excel Discussion (Misc queries) | |||
Looping | Excel Discussion (Misc queries) | |||
Looping | Excel Programming | |||
Looping | Excel Programming | |||
looping to End | Excel Programming |