Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default nested for loops and end for

If i have a nested for loop and put an END FOR in the inner loop, will it
exit just the inner loop or both loops?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default nested for loops and end for

It's "Exit For" not "End For". And it will exit the inner For loop. HTH
Otto
"SandyR" wrote in message
...
If i have a nested for loop and put an END FOR in the inner loop, will it
exit just the inner loop or both loops?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default nested for loops and end for

Give it a try... Here is some code for you to follow through...

Sub TestLoops()
Dim i As Integer
Dim j As Integer

For i = 1 To 3
For j = 1 To 100
MsgBox i * j
If j = 3 Then Exit For
Next j
Next i
End Sub
--
HTH...

Jim Thomlinson


"SandyR" wrote:

If i have a nested for loop and put an END FOR in the inner loop, will it
exit just the inner loop or both loops?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default nested for loops and end for

"End For" is a syntax error. Exit For will exit only the For...Next
structure that it's embedded in.

From XL/VBA Help ("Exit Statement"):
Exit For
Provides a way to exit a For loop. It can be used only in a
For...Next or For Each...Next loop. Exit For transfers control to
the statement following the Next statement. When used within nested
For loops, Exit For transfers control to the loop that is one
nested level above the loop where Exit For occurs.


One alternative if you want to exit both loops:

Dim a(1 To 100, 1 To 1000)
Dim i As Long, j As Long
Do
For i = 1 To 100
For j = 1 To 1000
a(i, j) = Int(Rnd() * 100)
If a(i, j) = 98 Then Exit Do
Next j
Next i
Loop
Debug.Print i, j

or

Dim a(1 To 100, 1 To 1000)
Dim i As Long, j As Long
For i = 1 To 100
For j = 1 To 1000
a(i, j) = Int(Rnd() * 100)
If a(i, j) = 98 Then GoTo Continue
Next j
Next i
Continue:
Debug.Print i, j



In article ,
SandyR wrote:

If i have a nested for loop and put an END FOR in the inner loop, will it
exit just the inner loop or both loops?

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
Help with nested for loops [email protected] Excel Discussion (Misc queries) 5 May 2nd 07 05:30 PM
Help with nested for loops [email protected] Excel Worksheet Functions 5 May 2nd 07 05:30 PM
nested loops trouble gaba Excel Programming 3 November 1st 04 04:50 PM
Help on nested loops Jan Lukszo Excel Programming 1 July 29th 04 08:41 AM
Nested loops?? CG Rosén Excel Programming 1 June 22nd 04 08:07 PM


All times are GMT +1. The time now is 04:31 AM.

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"