Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default For Next Loop Question

Sometimes in using a For/Next Loop, I notice that there is just "Next"
and sometimes "Next and (whatever). Is there a difference? TIA

Greg

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default For Next Loop Question

It's probably good practice to write

for n=1 to 10
do something
next n

but it's exactly the same as

for n=10 to 10:do something:next

Reason for being good practice is

for n=1 to 10
for m = 1 to 10
for o=1 to 10
for p = 1 to 10
next
next
next
next

is harder to read!!! (and to debug - ESPECIALLY if the for next loops
are nested further apart than my example!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default For Next Loop Question

Aidan, thanks

Greg

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default For Next Loop Question

An interesting fact I learned not too long ago:



Sub ABC()
For n = 1 To 10
For m = 1 To 10
For o = 1 To 10
For p = 1 To 10
Debug.Print n, m, o, p
Next p, o, m, n

End Sub

--
Regards,
Tom Ogilvy


" wrote:

It's probably good practice to write

for n=1 to 10
do something
next n

but it's exactly the same as

for n=10 to 10:do something:next

Reason for being good practice is

for n=1 to 10
for m = 1 to 10
for o=1 to 10
for p = 1 to 10
next
next
next
next

is harder to read!!! (and to debug - ESPECIALLY if the for next loops
are nested further apart than my example!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default For Next Loop Question

Tom, thanks. It is amazing that even you discover some new tidbits.
Thanks for sharing.

Greg



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default For Next Loop Question

Learned by accident or from a post or somewhere else????

Curious minds want to know <g.



Tom Ogilvy wrote:

An interesting fact I learned not too long ago:

Sub ABC()
For n = 1 To 10
For m = 1 To 10
For o = 1 To 10
For p = 1 To 10
Debug.Print n, m, o, p
Next p, o, m, n

End Sub

--
Regards,
Tom Ogilvy

" wrote:

It's probably good practice to write

for n=1 to 10
do something
next n

but it's exactly the same as

for n=10 to 10:do something:next

Reason for being good practice is

for n=1 to 10
for m = 1 to 10
for o=1 to 10
for p = 1 to 10
next
next
next
next

is harder to read!!! (and to debug - ESPECIALLY if the for next loops
are nested further apart than my example!



--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default For Next Loop Question

From someone else's post. That's why were all really here <g

--
Regards,
Tom Ogilvy



"Dave Peterson" wrote in message
...
Learned by accident or from a post or somewhere else????

Curious minds want to know <g.



Tom Ogilvy wrote:

An interesting fact I learned not too long ago:

Sub ABC()
For n = 1 To 10
For m = 1 To 10
For o = 1 To 10
For p = 1 To 10
Debug.Print n, m, o, p
Next p, o, m, n

End Sub

--
Regards,
Tom Ogilvy

" wrote:

It's probably good practice to write

for n=1 to 10
do something
next n

but it's exactly the same as

for n=10 to 10:do something:next

Reason for being good practice is

for n=1 to 10
for m = 1 to 10
for o=1 to 10
for p = 1 to 10
next
next
next
next

is harder to read!!! (and to debug - ESPECIALLY if the for next loops
are nested further apart than my example!



--

Dave Peterson



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default For Next Loop Question

I missed that one. Thanks for sharing.

It kind of looks like something that Tushar would like/do <vbg.

Tom Ogilvy wrote:

From someone else's post. That's why were all really here <g

--
Regards,
Tom Ogilvy

"Dave Peterson" wrote in message
...
Learned by accident or from a post or somewhere else????

Curious minds want to know <g.



Tom Ogilvy wrote:

An interesting fact I learned not too long ago:

Sub ABC()
For n = 1 To 10
For m = 1 To 10
For o = 1 To 10
For p = 1 To 10
Debug.Print n, m, o, p
Next p, o, m, n

End Sub

--
Regards,
Tom Ogilvy

" wrote:

It's probably good practice to write

for n=1 to 10
do something
next n

but it's exactly the same as

for n=10 to 10:do something:next

Reason for being good practice is

for n=1 to 10
for m = 1 to 10
for o=1 to 10
for p = 1 to 10
next
next
next
next

is harder to read!!! (and to debug - ESPECIALLY if the for next loops
are nested further apart than my example!



--

Dave Peterson


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default For Next Loop Question

You don't need the control variable in the Next statement.

Next
works exactly the same way as
Next Var

That said, you should always use "Next Var" for documentation
purposes. It make debugging and maintaining code much simpler
when you can see what the Next is refering to, especially in long
procedures.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"GregR" wrote in message
oups.com...
Sometimes in using a For/Next Loop, I notice that there is just
"Next"
and sometimes "Next and (whatever). Is there a difference? TIA

Greg



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
Loop question N.F[_2_] Excel Discussion (Misc queries) 0 July 12th 07 08:02 PM
end with loop question Lee Hunter Excel Programming 2 November 3rd 05 08:14 PM
loop question dabith Excel Programming 6 June 13th 04 05:28 PM
One more loop question Patti[_5_] Excel Programming 11 June 6th 04 07:14 AM
another loop question Patti[_5_] Excel Programming 5 May 31st 04 07:43 AM


All times are GMT +1. The time now is 03:46 AM.

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

About Us

"It's about Microsoft Excel"