Add Navigation Buttons to Excel Worksheet

Last July, I posted sample Excel VBA code to navigate to the next or previous worksheet. If you're on the second sheet, you can click the Next button to go to the third sheet. Or, click the Back button to go to the first sheet.

In the comments for that blog post, Ron de Bruin suggested modifying the two navigation macros, so they test if the target sheet is hidden, before selecting it. Finally, only 14 months later, the revised code is ready. Hey, quality work takes time! ;-)

Excel Worksheet Navigation Code

Here's the Excel VBA code for the two macros -- GoSheetBack and GoSheetNext. If the next sheet is hidden, the code keeps going until it finds the next visible sheet. If it reaches the end of the sheet tabs in either direction, it jumps to the other end, and continues from there.

'==========================
Sub GoSheetNext()
Dim wb As Workbook
Dim lSheets As Long
Dim lSheet As Long
Dim lMove As Long
Dim lNext As Long
Set wb = ActiveWorkbook
lSheets = wb.Sheets.Count
lSheet = ActiveSheet.Index
lMove = 1
With wb
For lMove = 1 To lSheets - 1
lNext = lSheet + lMove
If lNext > lSheets Then
lMove = 0
lNext = 1
lSheet = 1
End If
If .Sheets(lNext).Visible = True Then
.Sheets(lNext).Select
Exit For
End If
Next lMove
End With
End Sub
'==========================
Sub GoSheetBack()
Dim wb As Workbook
Dim lSheets As Long
Dim lSheet As Long
Dim lMove As Long
Dim lNext As Long
Set wb = ActiveWorkbook
lSheets = wb.Sheets.Count
lSheet = ActiveSheet.Index
lMove = 1
With wb
For lMove = 1 To lSheets - 1
lNext = lSheet - lMove
If lNext < 1 Then
lMove = 0
lNext = lSheets
lSheet = lSheets
End If
If .Sheets(lNext).Visible = True Then
.Sheets(lNext).Select
Exit For
End If
Next lMove
End With
End Sub
'==========================

Download the Sample File

To see the detailed instructions, and to download the sample Navigation code workbook, please go to the Excel VBA Worksheet Macro Buttons page on the Contextures website.

Watch the Excel Tutorial Video

To see the steps for creating the navigation macro buttons, please watch this Excel tutorial video.

The (X=0) is a logical expression that will evaluate to True only when X is equal to zero and will be False the rest of the time. In VB, True has a numerical equivalent of -1 (by the way, this is the opposite of TRUE on a worksheet where it has a numerical equivalent of +1) and False has a numerical equivalent of -1. Inside the loop, X is calculated using the Mod operator… one is added to the current sheet index and then it compared to the number of sheets plus one… when they are equal, the Mod operator returns 0, but you cannot have a sheet index of 0, so only when X=0 do we want to add one to get it up to the minimum sheet index value allowed. When X=0, the expression inside the Do While statement becomes 0-(-1) which equals +1. For all other value, X gets returned as calculated because (X=0) will evaluate to zero.

You are welcome. I hope you were able to read through the typo I just noticed in my first answer to you. I wrote “False has a numerical equivalent of -1? which was supposed to have said “False has a numerical equivalent of 0?. Sorry for any confusion this may have caused to you (or others).

I want to these button (NEXT & PREVIOUS) on user form to user click on NEXT button and retieve records from SQL SERVER and populate in corresponding textbox.
Please tell me on my email ‘sujeet.ansh2fab@gmail.com’.Its very urgent for me.
Thanks in advance..