ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Exit two loops at once (https://www.excelbanter.com/excel-programming/435194-exit-two-loops-once.html)

Jan Kronsell

Exit two loops at once
 

Is it possibble to exit two for loops at once?

I have some code like this

Sub INCC()
Set rng = ActiveSheet.UsedRange.Cells
For c = 1 To rng.Columns.Count
For r = 1 To rng.Rows.Count
If something Then
Some code here....
Exit For
End If
Next r
If something Then Exit For
Next c
more code here.....
End Sub

Now I was wandering if its possible to exit both loops if the condtion in
the inner loop is met, without having to test again in to leave the outer
loop.

Jan



Els

Exit two loops at once
 
You could type under the loops a label (LabelName:)
and in your loop: GoTo LabelName


"Jan Kronsell" schreef in bericht
...

Is it possibble to exit two for loops at once?

I have some code like this

Sub INCC()
Set rng = ActiveSheet.UsedRange.Cells
For c = 1 To rng.Columns.Count
For r = 1 To rng.Rows.Count
If something Then
Some code here....
Exit For
End If
Next r
If something Then Exit For
Next c
more code here.....
End Sub

Now I was wandering if its possible to exit both loops if the condtion in
the inner loop is met, without having to test again in to leave the outer
loop.

Jan




Jan Kronsell

Exit two loops at once
 
I know tthat but I try to avoid GoTo statements.

Jan

Els wrote:
You could type under the loops a label (LabelName:)
and in your loop: GoTo LabelName





Dave Peterson

Exit two loops at once
 
I set a flag and check it....

Sub INCC()
dim rng as range
dim c as long
dim r as long
dim FoundIt as boolean

Set rng = ActiveSheet.UsedRange.Cells
foundit = false
For c = 1 To rng.Columns.Count
For r = 1 To rng.Rows.Count
If something Then
Some code here....
foundit = true
Exit For
End If
Next r
If foundit = true Then
Exit For
end if
'more code here (maybe...)
Next c
more code here.....
End Sub

Jan Kronsell wrote:

Is it possibble to exit two for loops at once?

I have some code like this

Sub INCC()
Set rng = ActiveSheet.UsedRange.Cells
For c = 1 To rng.Columns.Count
For r = 1 To rng.Rows.Count
If something Then
Some code here....
Exit For
End If
Next r
If something Then Exit For
Next c
more code here.....
End Sub

Now I was wandering if its possible to exit both loops if the condtion in
the inner loop is met, without having to test again in to leave the outer
loop.

Jan


--

Dave Peterson


All times are GMT +1. The time now is 02:53 PM.

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