Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I Offset of Excel Tabs similar to ActiveCell.Offset

Status
Not open for further replies.

senators40

Technical User
Jan 7, 2003
68
CA
I have a number of tabs that in an excel file with many different names. Can I create a macro that goes to the left or right of the active tab similar to the ActiveCell.Offset feature in an excel workbook without entering in the tab or sheet name.

Thanks in advance

Jeff
 
Hi,

Code:
Sub TabLeft()
    If ActiveSheet.Index = 1 Then
        idx = Worksheets.Count
    Else
        idx = ActiveSheet.Index - 1
    End If
    Sheets(idx).Select
End Sub
Sub TabRight()
    If ActiveSheet.Index = Worksheets.Count Then
        idx = 1
    Else
        idx = ActiveSheet.Index + 1
    End If
    Sheets(idx).Select
End Sub
You could assign TabRight to shift+ctrl+R etc

Hope this helps :)

Skip,
Skip@TheOfficeExperts.com
 
A little less code than Skip's and it does exactly what you want.

To go one sheet forward use:

Code:
Sub SheetOffset_Next()
On Error Resume Next
Sheets(ActiveSheet.Index + 1).Select
On Error GoTo 0
End Sub

To go one sheet back use:
Code:
Sub SheetOffset_Previous()
On Error Resume Next
Sheets(ActiveSheet.Index - 1).Select
On Error GoTo 0
End Sub

Due to the errror trapping, no page index checking is needed.

Sorry Skip!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Sorry for butting in. :)



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
senators40,

To make this even easier, you can use the SendKeys function. Besides, my previous code, doesn't take into account that some sheets might be hidden. You should use the following code.

The keyboard shortcut to goto the next sheet is Ctrl+PgUp and Ctrl+PgDn to goto the previous sheet.

Code:
Sub NextSheet()
' goes to the next visible worksheet
Application.SendKeys ("^{PgUp}")
End Sub

Sub PreviousSheet()
' goes to the previous visible worksheet
Application.SendKeys ("^{PgDn}")
End Sub

I hope this helps!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top