I really loved your video tutorial on:
Password Protect & Hide Worksheets in Excel
I had a question and have been trying to add the ability to use the same code you suggested below but with adding a user defined password or without using a password (similar to the Excel's own 'protect/unprotect icon' which allows you to optionally use your own password or none at all)
I am stumped but if anyone knows how to modify that would be great!
Here is your original code below:
-----------
' Excel VBA Tutorial - https://www.TeachExcel.com
'
' Hide Worksheets Using a Password
'
' Specific Sheets will not be visible until the user enters the correct password.
'
' If you don't password protect the project, the user can still unhide the
' worksheet.
'
' Password Protect the Project: Right-click the Project in the Project Explorer >
' VBAProject Properties > Protection tab > check Lock project for viewing and
' input a password and hit OK.
Option Explicit ' Require that variables are declared.
' The Password
' A global constant value allows you to store this password once for your entire
' project and use it wherever you need to check for the password. This ensures
' that you only have to change the password in one place in the future.
Public Const conSheetPassword As String = "123456"
Sub ViewSheet()
' Code from: https://www.TeachExcel.com
'
' Unlock/show the worksheet if the user inputs the correct password.
' Variables.
Dim userInput As Variant
' Ask the user for the password to unlock the sheet.
userInput = InputBox( _
Prompt:="Input a password to unlock the worksheets.", _
Title:="Password Input", _
Default:="Password")
' Check if the correct password was input.
' - Trim() removes any space that might have been accidentally added to the
' start or end of the password.
' - LCase() ensures that a case-insensitive check is being performed because
' it converts the input and the password both to lower case.
If LCase(Trim(userInput)) = LCase(conSheetPassword) Then
' Pass - all is good.
' Show the Sheet.
Sheets("Helper").Visible = xlSheetVisible
Sheets("Raw").Visible = xlSheetVisible
' Go to the sheet.
Sheets("Helper").Select
Else
' Fail - wrong password.
' Let the user know.
MsgBox "Incorrect password. Access Denied."
End If
End Sub
Sub HideSheet()
' Code from: https://www.TeachExcel.com
'
' Hide the worksheet so that the user cannot unhide it by hand.
'
' If you don't password protect the project, the user can still unhide the
' worksheet.
'
' Password Protect the Project: Right-click the Project in the Project Explorer >
' VBAProject Properties > Protection tab > check Lock project for viewing and
' input a password and hit OK.
' Hide the sheet so the user can't unhide it by hand (so long as the Project
' code here is also password protected).
Sheets("Helper").Visible = xlSheetVeryHidden
Sheets("Raw").Visible = xlSheetVeryHidden
End Sub
-------