SOLVED

Macro Using a Relative Function for all Sheets

Copper Contributor

I want to create a macro that goes to the next sheet, but I can only create a macro that goes to a specific sheet, regardless of whether or not I'm selecting the "Use Relative References" button when creating the macro. How can I create the macro function I want, perpetuating going to next sheet, from any sheet in the Workbook?

2 Replies

Hi Ana,

 

Please try this code below and find it also in the attached file.

Sub GoToTheNextSheet()
    
    Dim nextSheetIndexNumber As Integer
    nextSheetIndexNumber = ActiveSheet.Index + 1
    
    If nextSheetIndexNumber > Sheets.Count Then
        Sheets(1).Activate
    Else
        Sheets(nextSheetIndexNumber).Activate
    End If

End Sub

 

This code allows you to go to the next sheet of the active sheet.

If the active sheet is the last sheet in the workbook, this code will move you to the very first sheet.

 

Hope that helps

best response confirmed by Ana McCorkhill (Copper Contributor)
Solution
Thank you!
Had to tweak the macro a little bit to get the code to work exactly as I was wanting:

Sub NextSheet()
'
' NextSheet Macro
'
' Keyboard Shortcut: Ctrl+Shift+L
'
nextSheetIndexNumber = ActiveSheet.Index + 1

If nextSheetIndexNumber > Sheets.Count Then
Sheets(1).Activate
Else
Sheets(nextSheetIndexNumber).Activate
End If

End Sub
1 best response

Accepted Solutions
best response confirmed by Ana McCorkhill (Copper Contributor)
Solution
Thank you!
Had to tweak the macro a little bit to get the code to work exactly as I was wanting:

Sub NextSheet()
'
' NextSheet Macro
'
' Keyboard Shortcut: Ctrl+Shift+L
'
nextSheetIndexNumber = ActiveSheet.Index + 1

If nextSheetIndexNumber > Sheets.Count Then
Sheets(1).Activate
Else
Sheets(nextSheetIndexNumber).Activate
End If

End Sub

View solution in original post