For each worksheet going forward

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

For each worksheet going forward

Post by VegasNath »

I am using code like:

Code: Select all

Sub Update()

.........

ActiveSheet.Next.Select
Call Update

End Sub
How can I suppress the error when the last worksheet has been updated, and there is no "next sheet"?
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: For each worksheet going forward

Post by HansV »

You could test like this:

Code: Select all

If ActiveSheet.Index = Sheets.Count Then
  Exit Sub
End If
ActiveSheet.Next.Select
Call Update
But why don't you use code like this:

Code: Select all

Dim wsh As Worksheet
For Each wsh In Worksheets
  ... ' do something with wsh
Next wsh
or

Code: Select all

Dim i As Integer
For i = 1 To Worksheets.Count
  ... ' do something with Worksheets(i)
Next i
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: For each worksheet going forward

Post by VegasNath »

HansV wrote:But why don't you use code like this:

Code: Select all

Dim wsh As Worksheet
For Each wsh In Worksheets
  ... ' do something with wsh
Next wsh
That would be my usual approach, but I don't want to affect all sheets, only the active sheet and those (tabs) to the right.

Thanks, the test should be sufficient. :cheers:
:wales: Nathan :uk:
There's no place like home.....

User avatar
rory
5StarLounger
Posts: 818
Joined: 24 Jan 2010, 15:56

Re: For each worksheet going forward

Post by rory »

Code: Select all

For n = activesheet.index to Sheets.count
   ' do something to sheets(n)
Next n
should also work.
Regards,
Rory