Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
Els Els is offline
external usenet poster
 
Posts: 3
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default 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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit Field vs Exit Button...... roy_ware Excel Programming 2 October 10th 07 04:05 PM
"Continue" and Exit with For Loops Andrew Hall NZ Excel Programming 4 November 23rd 06 02:44 AM
Loops... Willabo Excel Discussion (Misc queries) 2 June 14th 06 04:08 PM
do loops saravanan Excel Worksheet Functions 0 June 13th 06 10:53 AM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM


All times are GMT +1. The time now is 07:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"