Excelerate2
New Member
- Joined
- Mar 18, 2022
- Messages
- 25
- Office Version
- 365
- 2019
- 2016
- 2010
- Platform
- 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
-------