VBA to unlock sheet

jimmytiler

New Member
Joined
Aug 16, 2020
Messages
5
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
I have used vba code to lock the sheet, however, when I use the 'Unlock' code it seems to unlock the sheet regardless of the password being correct??

Code:
Sub LockSheet()

Dim WS As Worksheet
Set WS = Worksheets("Sheet1")

WS.Protect Password:="123456"
MsgBox "Data is locked"

End Sub

Sub UnlockSheet()

Dim WS As Worksheet
Set WS = Worksheets("Sheet1")

Ws.Unprotect Password:=InputBox("Enter password")

MsgBox "Data is unlocked"

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Are you sure Sheet1 is protected, with a password, before you run UnlockSheet?

If you run UnlockSheet, and get the message "Data is unlocked" no matter what password you enter, this implies that Sheet1 was not protected, or that Sheet1 was protected but without a password.
 
Upvote 0
Have you tried closing right out of Excel and then start up again?

In any case, with that code, if you provide an incorrect password the code should actually error like this (so your code really needs some method to deal with that circumstance)

1699503763231.png
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,099
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