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.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Please try the following on a copy of your workbook - assign it to all "continue" buttons.
VBA Code:
Sub Hide_Unhide()
    Dim i As Long
    i = ActiveSheet.Index
    If i = Worksheets.Count Then
        Worksheets(1).Visible = True
        Worksheets(i).Visible = False
    Else
        Worksheets(i + 1).Visible = True
        Worksheets(i).Visible = False
    End If
End Sub
 
Upvote 0
I suppose I should have mentioned, hide all the sheets except the Welcome sheet before running the code.
 
Upvote 0
Copy the following Sub into ThisWorkbook:
VBA Code:
Sub HideSheets()
Dim wb As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
Set wb = ThisWorkbook: Set sht1 = wb.Sheets(1): Set sht2 = wb.Sheets(2): Set sht3 = wb.Sheets(3)
Select Case ActiveSheet.Name
    Case sht1.Name
        sht2.Visible = xlSheetVisible
        sht1.Visible = xlSheetHidden
        sht2.Activate
    Case sht2.Name
        sht3.Visible = xlSheetVisible
        sht2.Visible = xlSheetHidden
        sht3.Activate
    Case sht3.Name
        sht1.Visible = xlSheetVisible
        sht3.Visible = xlSheetHidden
        sht1.Activate
End Select
End Sub
Then in each Sheet the following for ActiveX Command Buttons:
VBA Code:
Private Sub CommandButton1_Click()
 ThisWorkbook.HideSheets
End Sub
 
Upvote 0
I suppose I should have mentioned, hide all the sheets except the Welcome sheet before running the code.
Place this in ThisWorkbook:
VBA Code:
Private Sub Workbook_Open()
Dim wb As Workbook, sht As Worksheet, n As Integer
Set wb = ThisWorkbook
For n = 2 To wb.Sheets.Count
    wb.Sheets(n).Visible = xlSheetHidden
Next n
End Sub
 
Upvote 0
Please try the following on a copy of your workbook - assign it to all "continue" buttons.
VBA Code:
Sub Hide_Unhide()
    Dim i As Long
    i = ActiveSheet.Index
    If i = Worksheets.Count Then
        Worksheets(1).Visible = True
        Worksheets(i).Visible = False
    Else
        Worksheets(i + 1).Visible = True
        Worksheets(i).Visible = False
    End If
End Sub
I get the following message:
License information for this component not found. You don't have an appropriate license to use this functionality in the design environment (Error 429)


License information for this component not found. You don't have an appropriate license to use this functionality in the design environment (Error 429)

You are not a licensed user of the ActiveX control. This error has the following cause and solution:
  • You tried to place an ActiveX control on a form at design time or tried to add a form with an ActiveX control on it to a project, but the associated information in the registry could not be found.
    The information in the registry may have been deleted or become corrupted. Reinstall the ActiveX control or contact the control vendor.
 
Upvote 0
Please try the following on a copy of your workbook - assign it to all "continue" buttons.
VBA Code:
Sub Hide_Unhide()
    Dim i As Long
    i = ActiveSheet.Index
    If i = Worksheets.Count Then
        Worksheets(1).Visible = True
        Worksheets(i).Visible = False
    Else
        Worksheets(i + 1).Visible = True
        Worksheets(i).Visible = False
    End If
End Sub
I get the following message:
License information for this component not found. You don't have an appropriate license to use this functionality in the design environment (Error 429)


License information for this component not found. You don't have an appropriate license to use this functionality in the design environment (Error 429)

You are not a licensed user of the ActiveX control. This error has the following cause and solution:

  • You tried to place an ActiveX control on a form at design time or tried to add a form with an ActiveX control on it to a project, but the associated information in the registry could not be found.
    The information in the registry may have been deleted or become corrupted. Reinstall the ActiveX control or contact the control vendor.
 
Upvote 0
Copy the following Sub into ThisWorkbook:
VBA Code:
Sub HideSheets()
Dim wb As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
Set wb = ThisWorkbook: Set sht1 = wb.Sheets(1): Set sht2 = wb.Sheets(2): Set sht3 = wb.Sheets(3)
Select Case ActiveSheet.Name
    Case sht1.Name
        sht2.Visible = xlSheetVisible
        sht1.Visible = xlSheetHidden
        sht2.Activate
    Case sht2.Name
        sht3.Visible = xlSheetVisible
        sht2.Visible = xlSheetHidden
        sht3.Activate
    Case sht3.Name
        sht1.Visible = xlSheetVisible
        sht3.Visible = xlSheetHidden
        sht1.Activate
End Select
End Sub
Then in each Sheet the following for ActiveX Command Buttons:
VBA Code:
Private Sub CommandButton1_Click()
 ThisWorkbook.HideSheets
End Sub
Where do I copy the Private sub. Pardon me but I am completely new to VBA
 
Upvote 0
Instead of using ActiveX controls on your sheet, try using form controls instead.
Developer / Insert / Form Controls & select the form button from that section. Then assign the macro to the button.
 
Upvote 0
My suggested code is not a private sub. From the developer screen (press Alt+F11) select Insert / Module & copy paste the code in the blank section on the right of the screen.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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