Modify my VBA to Lock Workbook w/ user Password

Status
Not open for further replies.

Excelerate2

New Member
Joined
Mar 18, 2022
Messages
25
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows

Modify my VBA to Lock Workbook with a user Password​

I would like to use the same code below but instead of a hard coded password I would like to allow the user to create a password - (similar to the Excel's own 'protect/unprotect Workbook icon' which allows you to optionally use your own password or none at all to 'lock' the workbook to lock the sheet tabs).

I am stumped but if anyone knows how to modify it would be great!

Here is the code below (it has a fixed password: "123", but I would like the user to be able to create their own password, and maybe also opt for blank or no password:

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

-------
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Duplicate to: VBA to Protect & Hide Worksheets in Excel w/ user Password

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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