Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Password Protect & Hide Worksheets in Excel Video - Add custom password?

0

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
 
-------
Answer
Discuss

Answers

0
Selected Answer

Hi Excelerate and welcome to the Forum.

One way to allow a user to specifiy a password or none (without hard-coding it into the procedures) is to store the password which is invisible to the user (assuming they have no knowledge or VBA or you have protected your VBA project).

In the attached file, there's a VeryHidden worksheet called "User Data" in which cell B2 stores a passord (="123456" as it leaves me). You could name that cell of course.

Then the declaration: 

Public Const conSheetPassword As String


is changed (so it's not a Constant but a Public string varaible), as in bold below plus a the changes in bold below to the start of this sub:

Public conSheetPassword As String


Sub ViewSheet()
' Code from: https://www.TeachExcel.com
'
' Unlock/show the worksheet if the user inputs the correct password.

    ' Variables.
    
    
    Dim userInput As Variant
    'get the user's password (if any)
    conSheetPassword = Sheets("UserData").Range("B2").Value

    ' Ask the user for the password to unlock the sheet.
    If Len(conSheetPassword) > 0 Then
        userInput = InputBox( _
            Prompt:="Input a password to unlock the worksheets.", _
            Title:="Password Input", _
            Default:="Password")
        Else
        userInput = ""
    End If

(which checks if there's a password or not and asks for it if so).  The rest of that sub is unchanged.

The end of the HideSheet macro then needs changing as follow (changes in bold)

Sub HideSheet()
' Code from: https://www.TeachExcel.com
'
'...< <<unchanged code not shown>>>
    ' check if the user wants to add or change the password
    StorePassword

    ' Hide the sheets 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
    ' make sheet including password hidden too
    Sheets("UserData").Visible = xlSheetVeryHidden
End Sub

where it calls a new sub:

Sub StorePassword()

Dim Ans As String
' Ask if a new password is needed (where the argument returns a strring)
Ans = InputBox("Want to enter a NEW password? (Press Cancel to keep old)", "Set NEW password (if needed)or clear", "Password", 2)
' if it was changed, store it in hidden sheet
If Ans <> "Password" And Ans <> conSheetPassword Then Sheets("UserData").Range("B2").Value = Ans

End Sub

which displays a simple InputBox where the password can be kept (using Cancel), changed by typing something or no password set (by deleting the default text Password)

At the Workbook level, I added two event macros to ensure the file is saved with sheets hidden (in case it is later opened with macros disabled) and sets the variable when the workbook is opened.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'Hide sheets (in case file can be re-opened without macros enabled)
Sheets("Helper").Visible = xlSheetVeryHidden
Sheets("Raw").Visible = xlSheetVeryHidden
Sheets("UserData").Visible = xlSheetVeryHidden

End Sub

Private Sub Workbook_Open()

conSheetPassword = Sheets("UserData").Range("B2").Value

End Sub

Hope this fixes your problem.

Discuss

Discussion

Yes I'm trying to replace the 'hard coded password' with vba that allows the user to create their own password, and also possibly opt for no password (ie: like the Excel's 'protect/unprotect Workbook' icon does).
Excelerate (rep: 4) May 27, '22 at 11:41 am
I modified the end of my answer (for no password) so no InputBox appears.

I don't have any time today or this weekend to consider the user input password. Am I right to assume your users have any knowldge of VBA?
John_Ru (rep: 6142) May 27, '22 at 12:15 pm
Thanks, this would just be for the 'end user' to be able to enter their own password (instead of hard coded password) to lock/unlock Workbook. Basically just like Excel's own 'Protect/Unprotect Workbook icon' but in VBA. Thanks...
Excelerate (rep: 4) May 27, '22 at 12:22 pm
Forgot to say, the video is by Don (and most are) so he might have time to respond before me.
John_Ru (rep: 6142) May 27, '22 at 5:39 pm
Sure, any help would be great... thanks!
Excelerate (rep: 4) May 30, '22 at 6:24 pm
May have time to look at this again tomorrow but the solution will be less secure than "normal" protection within Excel. Also, do you intend to have a workbook per user? (Shared workbooks would be harder to protect) 
John_Ru (rep: 6142) May 30, '22 at 7:03 pm
Hello, sure no problem, the vba I have actually works perfect for me, but I just need to replace the hard-coded password method with a way that allows the user to optionally use their own password of their own choice (and they can also opt for no password).
Excelerate (rep: 4) May 31, '22 at 12:18 am
Please see my revised Answer (and file). Don't forget to mark the Answer as Selected if it works for you.
John_Ru (rep: 6142) May 31, '22 at 6:09 pm
Thanks! I see you have come up with a solution! I see the 2 sample files available but when I click them, they won't show. Anyway, here is my vba that I need to change to that 'user-defined password', maybe it can be applied to this?:
--------
Option Explicit

Public Const conSheetPassword As String = "123"

Sub ViewSheet()

Dim userInput As Variant

userInput = InputBox( _
Prompt:="Input a password to unlock the worksheets.", _
Title:="Password Input", _
Default:="Password")

If LCase(Trim(userInput)) = LCase(conSheetPassword) Then
' Pass - all is good.

Sheet2.Visible = xlSheetVisible
Sheet6.Visible = xlSheetVisible

Sheet2.Select

Else

MsgBox "Incorrect password. Access Denied."

End If

End Sub
-------------------------
Sub HideSheet()

Sheet2.Visible = xlSheetVeryHidden
Sheet6.Visible = xlSheetVeryHidden

Sheet1.Select

End Sub

-------

Thanks again, and sorry I'm a bit of a novice here, and would be great if they could change it at the input box area vs using another cell...
Excelerate (rep: 4) May 31, '22 at 6:24 pm
Not sure what you mean (I'd already removed the second file) and I'm about to go to bed.

The password CAN be changed in the InputBox (when you hide the data) by simply typing in a new password. It just gets stored in a cell.

If think I answered your question (and don't really have more time to spend on this). If you choose not to Select the answer, don't worry- it happens all the time (some people don't even respond to answers sadly)
John_Ru (rep: 6142) May 31, '22 at 6:40 pm
When you click on a file, your browser should download it to your Download folder. In Chrome for example, the download file appears at the bottom of the browser window. You can right click to see it in your folder and open it there (or directly from a right click option). It might differ for your browser.
John_Ru (rep: 6142) May 31, '22 at 6:47 pm
Ok I see, I'm sorry, I thought you would have to change it at the (B4 cell) which is why I didn't try the code. I will marked solved. Thank you very much!
Excelerate (rep: 4) May 31, '22 at 7:23 pm
Glad you got the file, it worked for you and thanks for selecting my answer 
John_Ru (rep: 6142) Jun 1, '22 at 2:09 am
Yes worked perfectly, thanks again for the excellent work and I really learned alot!
Excelerate (rep: 4) Jun 1, '22 at 2:14 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login