HOW TO GO TO NEXT WORKSHEET IN VBA?????

ajmckenna

Board Regular
Joined
Oct 7, 2002
Messages
145
A few questions. How do I get to the next worksheet in a VBA macro?

I.e. I have created a "next" macro to perform the same functions on the ActiveSheet but I can't get the macro make the next worksheet active

Is there a Next.Sheet command or I have tried this
Workbooks(ActiveSheet +1).Select but it is not working. Any ideas?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Sub MoveNext()
On Error Resume Next
Sheets(ActiveSheet.Index + 1).Activate
If Err.Number <> 0 Then Sheets(1).Activate
End Sub
 
Upvote 0
Try this:

If ActiveSheet.Index = Worksheets.Count Then
Worksheets(1).Select
Else
ActiveSheet.Next.Select
End If
 
Upvote 0
It's actually kind of an interesting "code golf" type problem:

Going forwards:
Worksheets(ActiveSheet.index Mod Worksheets.Count + 1).Select

Going backwards:
Worksheets((ActiveSheet.index + 2) Mod Worksheets.Count + 1).Select
 
Upvote 0
I would be interested to get feedback on this approach.

Would it not be possible to set the workbook and then the sheets and address the sheets individually?

Something like

Code:
[TABLE="width: 579"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Dim wb1 As Workbook[/TD]
[/TR]
[TR]
[TD]Set wb1 = ThisWorkbook[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Dim Sh1 As Worksheet[/TD]
[/TR]
[TR]
[TD]Set Sh1 = wb1.Worksheets("Sheet 1")

[TABLE="width: 579"]
<tbody style="background-attachment: scroll; background-clip: border-box; background-color: transparent; background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: repeat; background-size: auto; color: rgb(34, 34, 34); font-family: Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif; font-size: 13px; font-size-adjust: none; font-stretch: 100%; font-style: normal; font-variant: normal; font-weight: 400; line-height: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">[TR="bgcolor: transparent"]
[TD]Dim Sh2 As Worksheet[/TD]
[/TR]
[TR="bgcolor: transparent"]
[TD]Set Sh2 = wb1.Worksheets("Sheet 2")[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Yes, this is 11½ years old, but this answer is genius and won't error.
It can if if there are hidden sheets.
Same applies to code per post # 8 - can produce an error unless all sheets are visible
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,915
Members
448,532
Latest member
9Kimo3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top