Macro Unlock and unhide sheets with Password

PhillB

New Member
Joined
Sep 15, 2008
Messages
6
Hi. I have a macro to Password protect and hide sheets but cannot get one to unlock and unhide them to work. I am using the code below to lock the workbook and hide the sheets

Code:
Sub Lock1()
Sheets("2018").Select
ActiveWindow.SelectedSheets.Visible=False
Sheets("Comments").Select
ActiveWindow.SelectedSheets.Visible-False
Sheets("Sheet1").Select
Range ("D8").Select
ActiveWorkbook.Protect password="holidays",Structure:=True, Windows:=True
Sheets("Sheet1").Select
End Sub

Can anyone help with the unlock/unhide code?
Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Code:
Sub Lock1()
Sheets("2018").Visible = False
Sheets("Comments1").Visible = False
ActiveWorkbook.Protect Password = "holidays", Structure:=True, Windows:=True
End Sub
 
Last edited:
Upvote 0
Hi. Thanks. Just a typing error for this code as it wouldn't let me copy and paste for some bizzare reason. The lock code works fine, it's just the unlock one that I cannot work out.
 
Upvote 0
How about
Code:
Sub Lock1()
ActiveWorkbook.Unprotect Password = "holidays"

Sheets("2018").Visible = True
Sheets("Comments").Visible = True
Sheets("Sheet1").Select
Range("D8").Select
End Sub
 
Upvote 0
This throws up an error as it is trying to unlock before the password has entered. This is the macro I have been trying to use but this unlocks if anything is entered as a password.

Code:
On Error GoTo ErrorOccured
    Dim pwd1 As String, ShtName As String
    pwd1 = InputBox("Please Enter the password")
    If pwd1 = "" Then Exit Sub
    ShtName = "Workbook as a whole"

    ActiveWorkbook.Unprotect Password:="holidays"
    Sheets("2018").Visible = True
Sheets("Comments").Visible = True
Sheets("Sheet1").Select
Range("D8").Select
Exit Sub
    
ErrorOccured:
    MsgBox "Workbook could not be UnProtected - Password Incorrect"
    Exit Sub

End Sub
 
Upvote 0
Your locking code is incorrect - it's actually applying the password "False" (or the regional equivalent). The code should read (note the colon added):

Rich (BB code):
ActiveWorkbook.Protect password:="holidays",Structure:=True, Windows:=True
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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