For loop question

DeepUnderground
07-20-2010, 04:33 PM
HI I have the following:

For Each mysheet In Worksheets

mysheet.select

Next

In the workbook say there are 30 worksheets, but I only want to go through the first 5 sheets with the for loop. Is there another way to exit the for loop without adding an counter on the inside of the loop. For example:

i = 0
For Each mysheet In Worksheets

i = i+1

if i > 5 then
exit for
end if

mysheet.select

Next

Colin Legg
07-20-2010, 04:53 PM
Hi,

For that case, perhaps a different loop structure would be more appropriate?

Simple example:

Sub foo()

Dim i As Long

For i = 1 To 5
Sheets(i).Select
Next i

End Sub

DeepUnderground
07-20-2010, 07:41 PM
thankyou.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum