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

Display Password as Asterisk

0

Hallo, I just saw the video from TeachExcel about hide and unhiding sheets. But I don't know how to integrate the code from the link video to Mask the password from this exisiting code in Password Protect of hide and unhiding sheets.Please help me. Thank you.

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


The following are the URL of the video I watched.
https://www.youtube.com/watch?v=bMAlW_U24tk Password Protect & Hide Worksheet
https://www.youtube.com/watch?v=Y2k3uAM6N-A Make a Password Promt in Excel

My concern is I am a newly learning on VBA coding. And as of the moment I don't know how to integrate that 2 videos in once code to solve my concern.
Thank you so much for helping.
Answer
Discuss

Discussion

Hi Pau and welcome to the Forum

It's easier to use a UserForm to facilitate a masked entry of a password (than use the InputBox method) but I'm not sure which "hide/unhide" Tutorial you mean...

Please EDIT your original Question to;
      1. provide the URL for that tutorial
      2. attach your current Excel .xlsm file (using the Add Files... button below the question text, when editting the question).

Once you've done that, I'll try to Answer within a day.
John_Ru (rep: 6607) Dec 5, '24 at 5:09 pm
Hallo John_Ru, thank you for your response. I already edited the question with URL on it.

Can you please help me on the existing code there that will replace the Userform code instead? My goal is just to input the password as Asterisk once unhiding the sheets. Thank you so much.
Pau (rep: 2) Dec 6, '24 at 1:04 pm
Pau- thanks but I don't see the URL in your question (nor the requested file) but please see my Answer.
John_Ru (rep: 6607) Dec 6, '24 at 1:26 pm
Add to Discussion

Answers

0
Selected Answer

Pau

It's easier to use a UserForm to facilitate a masked entry of a password (than use the InputBox method).

Although you haven't said, I believe you are referring to the TeachExcel tutorial here Require a Password to View Hidden Worksheets in Excel - VBA Tutorial

In the attached revised file, I modified this code so it reads as follows (see lines in bold):

Sub ViewSheet()
' Code from: https://www.TeachExcel.com

' Unlock/show the worksheet if the user inputs the correct password.
' launch UserForm to check password entered
    UserForm1.Show

End Sub

This launches a UserForm I created (similar to an InputBox) but TextBox! has its PasswordChar property set to "*" (so as you type the (current) password 123456, it appears as ****** instead).

When passwrod is entered and the OK button is clicked, that triggers this code ("behind" the UserForm1 object):

Private Sub CommandButton1_Click()
    ' 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(TextBox1.Text)) = 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

    ' close UserForm
    Unload Me

End Sub

All the code there is cut from the original ViewSheet code but is performed using the code entered (taken from the TextBox1.Text property in bold above) rather then that entered in a regular VBA InputBox.

The last bold lines just close UserForm1

If the UserForm is closed with the X button, nothing happens.

Revision #1 06 December 2024

The file now has this additional UserForm code:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    ' if Enter is pressed, axt as if OK button was clicked
    If KeyCode = vbKeyReturn Then Call CommandButton1_Click
End Sub

This means that if you type the password, you can quickly press the Enter key (or click the OK button on the form) to reveal the sheets.

Hope this solves your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

Wow, that's really cool!.
It's finally working.

Thank you so much. :-)
Pau (rep: 2) Dec 6, '24 at 2:11 pm
Great. Thanks for selecting my Answer, Pau.
John_Ru (rep: 6607) Dec 6, '24 at 2:14 pm
Add to Discussion
0

 Hello Pau and welcome to the forum,

Masking the input of an input box is not possible. However, you can achieve what you want by creating a UserForm as the input box. It's explained in this TeachExcel tutorial: Mask UserForm Entry with '' * '' An asterisk is the most common but you can use any character.

Also, I don't recommend using Trim() and LCase() in this situation. Passwords are case sensitive, by converting user input and the actual passwrod to lowercase defeats this; decreasing the security. By not using Trim() and LCase() forces the password entry to match exactly the actual password.

Update Dec. 6/24

Hello Pau,

Learning VBA is an on-going process, as you go forward you will always learn something new. This site has a wealth of information to help you learn. The second youtube link you mention is the same video I linked to, except my link is to the video on this site (teachexcel.com). In the attached file I have replicated what Don (the owner of teachexcel.com) shows in the tutorial.

The workbook has 3 worksheets – "Main", "Raw", and "Helper". I created buttons to run the macros. I used the "rounded rectangle" shape for the buttons. On the "Main" sheet both buttons are there – "Show Sheets" and "Hide Sheets". The "Raw" and "Helper" sheets only have the "Hide Sheets" button. The password I used in this example is "123456"; this can be changed in the macro. I used the " # " symbol as the masking character.

To prevent someone from gaining access to the password (and changing it) the VBA Project should also be protected as recommended by Don. This is easily done from within the editor window. At the top select the "Tools" menu and then "VBAProject Properties". In the window that opens select the "Protection" tab and select "Lock project for viewing" and enter a password.

If this solves things, please mark my answer as Selected.

Cheers   :-)

Discuss

Discussion

Hallo WilliwD24, thank you for quick response. I just copied that code from TeachExcel. I am really a beginner on VBA and i find it interesting. Can you please help me revised that portion to solve my concern? As of the moment, I can't do it by myself as I am just newly learning all this thing. Thank you once again for your quick response.
Pau (rep: 2) Dec 6, '24 at 12:57 pm
@Willie (/Pau)- I agree with your comments on the (adverse) security aspects of using Trim and LCase but realised Pau is new to VBA so didn't confuse matters in my Answer. It would also be more secure to use a Workbook_BeforeClose macro (to ensure it is in the hidden state when next launched).
John_Ru (rep: 6607) Dec 6, '24 at 1:30 pm
@John
After posting my update I see that you beat me to the punch (again) while I was crafting my update.
Once again, nice job.

Cheers   :-)
WillieD24 (rep: 657) Dec 6, '24 at 3:00 pm
@Williethanks but I didn't mean to beat your effort, just provide a fuller solution for a new user. Once again you provided some informed and telling guidance (though perhaps a tad too much for a newbie). 
John_Ru (rep: 6607) Dec 6, '24 at 6:51 pm
@John,
No worries, I was just kidding around about the "beat me to the punch". By both of us providing a solution just adds more credence that there is more than one way to get the job done. (Plus, I too learn from your work)
WillieD24 (rep: 657) Dec 6, '24 at 10:01 pm
Add to Discussion


Answer the Question

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