'Setting up password using inputbox to hide/unhide excel sheet

I've recently made a MCQ test module that will help in preparing test papers with random questions from the question bank and conduct test and calculate the marks.

All of it has been coded and completed. Since this is MCQ test macro, there is a sheet named "Admin CP" that will do most of the work like generating questions, setting up timer, setting passing percentage etc.. This sheet will be hidden using another simple macro xlveryhidden. In order to unhide this sheet, I want to set up something where user inputs the password in Admin CP and then hide the worksheets.

If the admin wants to access this sheet again, he'll have to input the password which will be validated with the one that was setup earlier.

Below is the screenshot of the code that I've written. The "IF, Else, Then and =" doesn't seem to work. It either throws the warning message of incorrect password or unhides the sheets even if the password is incorrect.

Any help is highly appreciated. Thanks in advance.

https://ibb.co/9Yyqh0V



Solution 1:[1]

Brother, you have not assigned any value to inpu2; so if you want to compare inpu1 with inpu2. you should assign previously stored values to inpu2. only then you should compare them both. you can accomplish this in two simple steps.

Step 1: create a new hidden sheet to store pass

Step 2: retrieve pass from that sheet to check

Option Compare Text

Sub passToLock()
    Dim newSheetObj As Worksheet, inpu2 As String
    Set newSheetObj = Worksheets.Add
    
    newSheetObj.Name = "hiddenPass"           ' to refer to later on
    newSheetObj.Visible = xlSheetVeryHidden   ' to make the sheet hidden
    inpu2 = Application.InputBox("Enter The Password") ' to get initial password
    newSheetObj.Range("A1").Value = inpu2     ' to store the password in hidden sheet
    
    Set newSheetObj = Nothing
End Sub

Sub Unlock2()

    Dim inpu1 As String, inpu2 As String
    
    inpu2 = Sheets("hiddenPass").Range("A1").Value
    inpu1 = Application.InputBox("Enter The Password")
    
    If inpu1 = inpu2 Then Worksheets("Admin CP").Visible = xlSheetVisible

End Sub

I am sure you can tweak the code to meet your demand.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s