• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Lock cell when another cell populated

I would like cell (B47:C47) to lock once a value >1 is entered into Cell N47. I already have a button on the page to lock and unlock the worksheet.
 
Hi lesley
Try this code in worksheet_Change
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$N$47" Then
        ActiveSheet.Unprotect 1
            If Target.Value > 1 Then
                Range("B47:C47").Locked = True
            Else
                Range("B47:C47").Locked = False
            End If
        ActiveSheet.Protect 1
    End If
End Sub
 

Attachments

  • Test.xlsm
    22.9 KB · Views: 1
Just little version of YasserKhalil

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$N$47" Then
        ActiveSheet.Unprotect 1
            Range("B47:C47").Locked = Target.Value > 1
        ActiveSheet.Protect 1
    End If
End Sub
 
Thank you very much both of you. I have multiple ranges which need to be locked will this be a case of just adding it to the list.

Code:
  If Target.Address = "$N$47" Then
  ActiveSheet.Unprotect 1
  Range("B47:C51").Locked = Target.Value > 1
  Range("E7:O9").Locked = Target.Value > 1
  ActiveSheet.Protect 1
  End If
 
Last edited by a moderator:
So try this modification
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$N$47" Then
        Dim Rng As Range
        Set Rng = Union(Range("B47:C51"), Range("E7:O9"))
        ActiveSheet.Unprotect 1
            Rng.Locked = Target.Value > 1
        ActiveSheet.Protect 1
    End If
End Sub

You can add more ranges in this line
Code:
Set Rng = Union(Range("B47:C51"), Range("E7:O9"))
 
Thanks for the reply, I am getting an error when, the debug is highlighting 'Rng.Locked= target.value >1' when I run the query.

'Unable to set the locked property of the range class'
 
Thanks for the reply, I am getting an error when, the debug is highlighting 'Rng.Locked= target.value >1' when I run the query.

'Unable to set the locked property of the range class'


Just tested with YasserKhalil test book & found working.

You might did something wrong there!!!
 

Attachments

  • Test.xlsm
    23.4 KB · Views: 2
Back
Top