Activating hidden Sheets using If statement.

AKOsman1

New Member
Joined
Nov 19, 2023
Messages
24
Office Version
  1. 2007
Platform
  1. Windows
I have multiple sheets in my project. At the beginning all the sheets are hidden except the "Welcome" Sheet. To continue the user must click the button which will Unhide the next Sheet, "CheckExpiry", I want him to be in and hide the "Welcome " sheet". To continue from the "CheckExpiry" sheet to the next sheet, the user must click the button which will unhide the next Sheet, "Password", I want him to be in and hide the "CheckExpiry" sheet. (and so on and on)
I have created a macro for each button on each sheet. I want to create a single macro to be assigned to all the "continue" buttons on all the sheets using the
"IF..IFELSE..END IF" statement; e.g.

if ActiveSheet = "Welcome" then
"CheckExpiry" = Unhide
"Welcome" = Hide
ElseIf ActiveSheet = "CheckExpiry" then
"Password" = Unhide
"CheckExpiry = Hide
End If

Ps. I have worked on basic Excel spreadsheets for some time but I am absolutely new to VBA. Kindly pardon me for my ignorance.
 
Try the following:
VBA Code:
Option Explicit
Sub Hide_Unhide()
    Application.ScreenUpdating = False
    Dim i As Long, j As Long, a
    a = Array("Welcome", "CheckExpiry", "Password") '<-- Put sheet names in the ORDER you want them to open
    i = Application.Match(ActiveSheet.Name, a, 0)
    If i = UBound(a) + 1 Then i = 1 Else i = i + 1
    For j = 1 To Worksheets.Count
        Worksheets(j).Visible = True
    Next j
    For j = 1 To Worksheets.Count
        If j <> i Then Worksheets(j).Visible = False
    Next j
    Application.ScreenUpdating = True
End Sub
Hi Kevin, I sincerely thank you for your help. It is apparent that either way the Sheets must be in order. That being the case I will use your first example.
Once again, many thanks.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Have a try with the following - shouldn't matter what order the sheets are in in the workbook, they should open in the order you have them in the code:
VBA Code:
Option Explicit
Sub Hide_Unhide_V2()
    Application.ScreenUpdating = False
    Dim i As Long, a, sh As String, ws As Worksheet
    a = Array("Welcome", "CheckExpiry", "Password") '<-- Put sheet names in the ORDER you want them to open
    i = Application.Match(ActiveSheet.Name, a, 0)
    If i = UBound(a) + 1 Then i = 1 Else i = i + 1
    sh = a(i - 1)
    For Each ws In Worksheets
        ws.Visible = True
    Next ws
    For Each ws In Worksheets
        If ws.Name <> sh Then ws.Visible = False
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi Kevin,
This code works perfectly. I moved the Sheets haphazardly around and still it followed the code sequence. Thanks for you assistance.
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

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