Using VBA Code To Hide and Unhide Sheets in Excel

seuntoba

New Member
Joined
Nov 7, 2014
Messages
20
Good Day Community,


I am looking for VBA Code on a project where i can use buttons to move between sheets. While moving between sheets i'll like only one sheet visible at any given time and use buttons to unhide and hide other sheets based on user action(button click)

Lets assume i have 4 main sheets - Main, Introduction, Discussion and Conclusion.


I will like to move from the Main sheet to the introduction sheet by clicking on a button in the "Main Sheet", for example Next.

When i get to the Introduction Sheet i'll like 2 buttons;

1- Back - Which returns to Main
2- Next - Which moves to the Discussion Sheet.
While doing this i'll like inactive sheets to be hidden


Under Discussion. i'll like 3 buttons
1- Back - returns to introduction
2. Home - Return to Main
3. Next - Moves to conclusion
While doing this i'll like inactive sheets to also be hidden


This way i can create many more sheets using the code. Everything i have tried has failed
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Perhaps something like this
Code:
Sub NextSheet()
    Dim ActiveIndex As Long
    ActiveIndex = ActiveSheet.Index
    With ThisWorkbook
        .Sheets((ActiveIndex Mod (.Sheets.Count)) + 1).Visible = xlSheetVisible
        .Sheets(ActiveIndex).Visible = xlSheetHidden
    End With
End Sub

Sub PreviousSheet()
    Dim ActiveIndex As Long, PrevIndex As Long
    ActiveIndex = ActiveSheet.Index
    With ThisWorkbook
        PrevIndex = ActiveIndex - 1: If PrevIndex = 0 Then PrevIndex = .Sheets.Count
        .Sheets(PrevIndex).Visible = xlSheetVisible
        .Sheets(ActiveIndex).Visible = xlSheetHidden
    End With
End Sub
 
Upvote 0
Thanks Mike, I haven’t tried this yet but is this code for each of my buttons or for the Worksheet Module?
 
Upvote 0
On each sheet you would put two buttons, one labeled "Next Sheet", the other "Previous Sheet".

The Next Sheet buttons would be attached to the NextSheet macro and the Previous Sheet buttons attached to the PreviousSheet macro
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

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