VBA code to lock range of cells based on a cell value

Jmoz092

Board Regular
Joined
Sep 8, 2017
Messages
184
Office Version
  1. 365
  2. 2011
Platform
  1. Windows
  2. MacOS
I'm having trouble locking a range of cells if a VBA password prompt does not enter specific user names into a cell. I want any users to be able to edit range A1:I11, but I only want 2 specific users to be able to edit anything else on the sheet (codename sheet1).

The user's name is determined at a password prompt and then entered in to cell K18. If the value of K18 does not equal "user1" or "user2", then I want to lock range A12:R61.

I can't get the code to keep the cells locked, and I think I'm also missing the worksheet_change function. Is there something similar for range_change??

I have this code inserted into Sheet1:

Code:
Sub worksheet_change(ByVal target As Range)


Dim pwR As Range
Set pwR = Range("K18")


If pwR <> "user1" Or pwR.Value <> "user2" Then

range("a1:I11").Locked = False
Range("a12:r61").Locked = True
MsgBox "You do not have authorization to edit these cells." & vbCr & "Only Zuul, user1, or user2 may edit these cells." & vbCr & "The range is now locked."
End If


End Sub
 
Last edited:
Click here to download your file. Range A1:I11 in Sheet1 is unlocked for data entry. The sheet is protected with the password "myPassword". The sheet must be protected in order to activate the locking of cells. Sheet2 contains the list of passwords (Zuul, user1, user2) and is xlVeryHidden. You can unhide it and add to or change the passwords. The macro is in the Workbook code module and I added some comments to explain what it is doing. Save the file, close it and re-open it. You will be prompted for the password. If you enter a valid password, A12:R61 will be unlocked and the password will be entered in K18. If you enter and invalid password, the user will have access to A1:I11 only.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Thank you for the help and example @mumps! I'll toy around with that and see if I can make it fit into our existing code and suit our needs.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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