Deceptively difficult? Lock a range of cells (block) after enter a specific value in one cell

lucian688

Board Regular
Joined
Mar 9, 2005
Messages
82
Hi I have spreadsheet & I am hoping to have a block of cells (e.g., A1:D10) locked upon changing a cell (e.g., F15 having an entry of "Yes"). I.e., any other value such as "No" will not lock cells A1:D10. Additionally, anyway to embed the code without having to run the macro each time opening up the spreadsheet would be nice.

Any tips are greatly appreciated! Thank you!!
Luke
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Locking cells does not block entries to them unless the sheet is protected. Is your sheet protected? If not, would protecting it be acceptable?

Entries can be made to unlocked cells on a protected sheet. A bit of event code could be used to automatically lock a designated block of cells whenever "Yes" is entered in a specific cell.
 
Upvote 0
I tried the following... 1st of all it didn't work & 2nd I think it only locks the entire worksheet not the block. Help needed!
_______________________

Worksheet_Change(ByVal Target As Range)

If Target.Address = Range ("F15").Address Then
If Target.Value = "Yes" Then
ActiveSheet.Unprotect Password = "password"
Else
ActiveSheet.Protect Password = "password", DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End If

End Sub
 
Upvote 0
So it seems that I should lock/protect the sheet 1st. Basically there are 2 users involved. User 2 is an approver & upon approving data entry (block of cells) by User 1, the block of cells should be locked - i.e., User 2 approve the entry & give it back to User 1.... User 1 cannot change the value anymore.

I tried the code above as a skeleton start & it went nowhere everything locked. :) haha
 
Last edited:
Upvote 0
See if this works for you. Change the password (in red font) to suit.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Cells.Locked = False
Me.Protect Password:="pswd", userinterfaceonly:=True
Set Target = Target.Cells(1, 1)
If Target.Address <> "$F$15" Then Exit Sub
Select Case UCase(Target.Value)
    Case "YES": Range("A1:D10").Locked = True
    Case Else: Range("A1:D10").Locked = False
End Select
End Sub
 
Last edited:
Upvote 0
Absolutely Brilliant! One last ques: if I want to have the data locked in another/adjacent worksheet "Sheet2" upon entry of "Yes" in "Sheet1" how would I adjust the value?
 
Upvote 0
Absolutely Brilliant! One last ques: if I want to have the data locked in another/adjacent worksheet "Sheet2" upon entry of "Yes" in "Sheet1" how would I adjust the value?
Qualify the sheet like this:

Case "YES": Sheets("Sheet2").Range("A1:D10").Locked = True

BTW in the single sheet case, you probably should lock F15 too so another user can't delete the "Yes" that the supervisor entered. If you want to do that try replacing the code I posted earlier with this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Cells.Locked = False
Me.Protect Password:="pswd", userinterfaceonly:=True
Set Target = Target.Cells(1, 1)
If Target.Address <> "$F$15" Then Exit Sub
Select Case UCase(Target.Value)
    Case "YES": Union(Range("A1:D10"), Range("F15")).Locked = True
    Case Else: Union(Range("A1:D10"), Range("F15")).Locked = False
End Select
End Sub
 
Upvote 0
I have another tag on question... thanks to Joe for the previous work btw.

So upon changing the fields to "YES", in addition to locking the block of cells. How can I enter a value in another cell such as "JOE". Then lock that cell

OR a simpler approach...

Upon changing the fields to "YES", add a value "JOE" to one of the cells on the worksheet then LOCK the entire sheet (password protected). So here is the idea - once the reviewer flick the field to "YES" ultimate goal is to lock the entire worksheet to prevent further tempering... but locking occurs immediately after a Name stamp is added to the worksheet for identification.

Also I assume the worksheet has to be "unlocked" prior to running the script...?

Thanks again!
 
Last edited:
Upvote 0
I have another tag on question... thanks to Joe for the previous work btw.

So upon changing the fields to "YES", in addition to locking the block of cells. How can I enter a value in another cell such as "JOE". Then lock that cell

OR a simpler approach...

Upon changing the fields to "YES", add a value "JOE" to one of the cells on the worksheet then LOCK the entire sheet (password protected). So here is the idea - once the reviewer flick the field to "YES" ultimate goal is to lock the entire worksheet to prevent further tempering... but locking occurs immediately after a Name stamp is added to the worksheet for identification.

Also I assume the worksheet has to be "unlocked" prior to running the script...?

Thanks again!
The script is triggered by changes to the worksheet, runs automatically, and never needs to be unprotected ("unlocked") to run. Your tag on question is vague at best. For example, where exactly is "another cell" on the worksheet.
 
Upvote 0
Okay let me give it another shot to clarify. So the idea is to add a name stamp "John" anywhere/any cell on the worksheet before the entire sheet is locked to prevent tempering.

So in the example above, by change F15 to "Yes", let's say F16 will print "John" then the entire worksheet is locked.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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