For Next Question

  • Thread starter Thread starter Fredg
  • Start date Start date
F

Fredg

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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top