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


I have a workbook that I use several For Next loops in. I was wonderin
if someone could explain the issues with using or not using th
vairable after the Next.

For example I have:

Count = 0
For Each cell In Selection
Count = Count + 1
cell.Value = "D" & Count
Next

If I put "Cell" after Next it works the same way.

Count = 0
For Each cell In Selection
Count = Count + 1
cell.Value = "D" & Count
Next cell

Which is the best programming style? I would think adding the variabl
name after the Next would be correct.

Thanks
Fre

--
Fred
-----------------------------------------------------------------------
Fredg's Profile: http://www.excelforum.com/member.php...fo&userid=2632
View this thread: http://www.excelforum.com/showthread.php?threadid=46675

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


Also, of the two blocks of codes which would be the most correct, bot
work the same way. I would think the second one, but I can not explai
WHY it would be better.

-----
Sub countsheets()

For Each Worksheet In ActiveWorkbook.Worksheets
MsgBox Worksheet.Name
Next

End Sub

-----

Sub countsheets()

Dim Item as WorkSheet
For Each Item In ActiveWorkbook.Worksheets
MsgBox Item.Name
Next Item

End Su

--
Fred
-----------------------------------------------------------------------
Fredg's Profile: http://www.excelforum.com/member.php...fo&userid=2632
View this thread: http://www.excelforum.com/showthread.php?threadid=46675

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

Putting the loop variable after the next makes it easier to see where the
loop begins and ends.

Using worksheet as a variable probably isn't the best of ideas. Declaring
your variable is good, so teh second is probably perferred.

--
Regards,
Tom Ogilvy

"Fredg" wrote in
message ...

Also, of the two blocks of codes which would be the most correct, both
work the same way. I would think the second one, but I can not explain
WHY it would be better.

-----
Sub countsheets()

For Each Worksheet In ActiveWorkbook.Worksheets
MsgBox Worksheet.Name
Next

End Sub

-----

Sub countsheets()

Dim Item as WorkSheet
For Each Item In ActiveWorkbook.Worksheets
MsgBox Item.Name
Next Item

End Sub


--
Fredg
------------------------------------------------------------------------
Fredg's Profile:

http://www.excelforum.com/member.php...o&userid=26328
View this thread: http://www.excelforum.com/showthread...hreadid=466757



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
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


All times are GMT +1. The time now is 06:17 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"