VBA to Protect & Hide Worksheets in Excel w/ user Password

Excelerate2

New Member
Joined
Mar 18, 2022
Messages
25
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
I had a question and have been trying to add the ability to use the same code below but with adding a user defined password and/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 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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Basically just need it to be like Excel's own 'Protect/Unprotect Workbook icon' but in VBA.
Thanks...
 
Upvote 0
Basically, just need to use my above code but replace the hard-coded password method with a user defined password
(To allow the user to optionally use their own password of their own choice, and also opt to use no password).
 
Upvote 0

Forum statistics

Threads
1,216,060
Messages
6,128,549
Members
449,458
Latest member
gillmit

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