How to VeryHidden worksheets and display them with a password

K0st4din

Active Member
Joined
Feb 8, 2012
Messages
488
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
I love this excel so much that he can't put it into words.
Macros, I adore them.
The truth is that I searched in many sites, but very rarely there is a macro that is 100% consistent with what the user needs.
I could not find for my needs and therefore I ask you for help.
Here's what I need:
I have a workbook with 50 worksheets with different names.
I'm looking for a macro that always leaves a worksheet with a name (base) and the others with names ("peaches", "bananas", etc.) to be VeryHidden after the workbook closes (maybe Me.Save) .
And when I open the workbook, only the worksheet (base) is visible and I can show all the hidden worksheets with a password.
If the password is incorrect, do not show any of the hidden worksheets
I will be grateful for any help.
Greetings
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,
See this example:

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'On workbook code, before close
Sheets(Array("Plan2", "Plan3")).Select
ActiveWindow.SelectedSheets.Visible = False
ActiveWorkbook.Save
End Sub

'/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/

'On a buttoncommand
Sub Botão1_Clique()
If InputBox("Password") <> 123 Then
    MsgBox "Incorrect pass, try again!"
    Else
    Sheets("Base").Select
    Sheets("Plan2").Visible = True
    Sheets("Plan2").Select
    Sheets("Plan3").Visible = True
End If
End Sub
 
Upvote 0
Something like this perhaps. You might want to use code names of the workbooks rather than the names of the workbooks, so that the code wont crash if the user renames the Base worksheet
VBA Code:
' in ThisWorkbook code module

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim oneSheet As Worksheet
    With Me
        .Worksheets("Base").Visible = xlSheetVisible
        For Each oneSheet In .Sheets
            If oneSheet.Name <> "Base" Then
                oneSheet.Visible = xlSheetVeryHidden
            End If
        Next oneSheet
    End With
End Sub


Private Sub Workbook_Open()
    Dim uiPassword
    Dim oneSheet As Worksheet
    Const soughtPassword As String = "password"
    uiPassword = Application.InputBox("Enter password", Default:=soughtPassword, Type:=2)
    If uiPassword = soughtPassword Then
        For Each oneSheet In Me.Sheets
            oneSheet.Visible = xlSheetVeryHidden
        Next oneSheet
    Else
        MsgBox "wrong password"
    End If
End Sub
 
Upvote 0
mikerickson

In the same module, do I put both macros or in separate ones?
I will try it and give back an answer, what happened!
Thank you very much!
 
Upvote 0
As Mike put at the top of the code, you put them both in the ThisWorkbook module ( Find your workbook in the VBA Project window, look down and you will see ThisWorkbook, right click and then click view code. Your codes both go in the window that appears).

' in ThisWorkbook code module
 
Upvote 0
Something like this perhaps. You might want to use code names of the workbooks rather than the names of the workbooks, so that the code wont crash if the user renames the Base worksheet
VBA Code:
' in ThisWorkbook code module

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim oneSheet As Worksheet
    With Me
        .Worksheets("Base").Visible = xlSheetVisible
        For Each oneSheet In .Sheets
            If oneSheet.Name <> "Base" Then
                oneSheet.Visible = xlSheetVeryHidden
            End If
        Next oneSheet
    End With
End Sub


Private Sub Workbook_Open()
    Dim uiPassword
    Dim oneSheet As Worksheet
    Const soughtPassword As String = "password"
    uiPassword = Application.InputBox("Enter password", Default:=soughtPassword, Type:=2)
    If uiPassword = soughtPassword Then
        For Each oneSheet In Me.Sheets
            oneSheet.Visible = xlSheetVeryHidden
        Next oneSheet
    Else
        MsgBox "wrong password"
    End If
End Sub
Hi, I tried the macro, but something needs to be touched.
I changed the name of the worksheet to mine.
When closed, all worksheets are hidden, except for worksheet 1 (as it should be).
But when opened, the window in which I have to write the password is displayed, and in this case, it is written by itself.
The typewriter window should be blank and I should write the password.
And for the finale, it shows me some mistake.
Ask for assistance.
Thanks in advance.
2020-05-22_071639.jpg
2020-05-22_071701.jpg
2020-05-22_071718.jpg
 
Upvote 0
I adjusted this to use the code name of the master sheet. And I corrected the variable to xlSheetVisible.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim oneSheet As Worksheet

    Sheet1.Visible = xlSheetVisible
    
     For Each oneSheet In Me.Sheets
         If oneSheet.Name <> Sheet1.Name Then
             oneSheet.Visible = xlSheetVeryHidden
         End If
     Next oneSheet
     
End Sub

Private Sub Workbook_Open()
    Dim uiPassword
    Dim oneSheet As Worksheet
    
    Const soughtPassword As String = "password"
    uiPassword = Application.InputBox("Enter password", Default:=soughtPassword, Type:=2)
    
    If uiPassword = soughtPassword Then
        For Each oneSheet In Me.Sheets
            oneSheet.Visible = xlSheetVisible
        Next oneSheet
    Else
        MsgBox "wrong password"
    End If
End Sub
 
Upvote 0
When I first learned how to make use of 'Hidden Sheets' I thought it was marvelous until. . . . The many and varied way you can get into the 'Hidden Sheets' made the whole exercise a total waste. Plus if you were to do a Move or Copy sheets, the Hidden Sheets were lost. I have found the use of well hidden "Helper" columns within the worksheet the best alternative. Best of luck and stay well.
 
Upvote 0
Hidden sheets are a good place to store static data, out of sight and out of the way.
They are a lousy way to keep secrets.
 
Upvote 0

Forum statistics

Threads
1,216,444
Messages
6,130,659
Members
449,585
Latest member
Nattarinee

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