Unlocking cells using VBA

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
I have a worksheet whose cells are locked. Now i want to create a button which when clicked prompts the user to input a password which if correct will unlock the cells but if the password is incorrect then it asks to renter the password.

How can i code this in VBA? (Im guessing a module needs to be created but i dont know how to code to achieve the above)
 

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.
Hi there,

A simple way of doing this would be:

Code:
Private Sub CommandButton1_Click()
    On Error Resume Next
    pass = InputBox("Please enter the password")
    ActiveSheet.Unprotect Password:=pass
    If Err.Number = 1004 Then MsgBox "Password Incorrect, please try again"
End Sub

You will assign your password in Tools -> Protection -> Protect Sheet. Using the way above users can not take a look in the VBA code and find out the password.
 
Upvote 0
Hi try the below code,
Code:
Sub UnProtectSheet()
strpassword = InputBox("Please provide the password.")
For i = 1 To 3   
'Currently 3 tries are allowed
'Current password used is MyPassword
 If strpassword = "MyPassword" Then    <change to="" your="" password=""><add the="" actual="" password="" here="">

    ActiveSheet.Unprotect Password:="MyPassword"  '<<add the="" actual="" password="" here="">
    Exit For
Else: 
  If not i=3 then
    strpassword = InputBox("You entered a wrong password.Enter correct password. Tries left=" & 3 - i)
  End If
    
End If
Next
End Sub
Thanks,
Ogo</add></add></change>
 
Last edited:
Upvote 0
thanks both worked perfectly. Just one more related question:

If i wanted to create another button to lock the sheets the code would be similar but i need to use:

ActiveSheet.Protect Password:="MyPassword"

Is this correct?
 
Upvote 0
Then the code can be something like this
Code:
Sub MyProtection()
    If ActiveSheet.Name = "Sheet1" Or ActiveSheet.Name = "Sheet2" Then
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=False, Password:="MyPassword"
    End If
End Sub
Or Simply
Code:
Sub MyProtection()
    
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=False, Password:="MyPassword"
  
End Sub
Thanks,
Ogo
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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