VBA Lock Cells in Range

tabbytomo

New Member
Joined
Jun 23, 2016
Messages
16
Hi everyone! I've hit a bit of a brick wall with my knowledge and could do with a hand. I've got a simple sheet, I need cells A2 - D2 filling in, then once there is content in that range, lock that range AND unlock the row below (A3 - D3).

So far, I've managed to get it to lock the entire sheet when there is content in the range, and this applies no matter how many rows you complete, so for example I could get all the way down to A20 - D20 and this code would work. I'm looking for some help with that final bit, keep the row below unlocked as that will be the next set of cells we want data to go into.

Any ideas?

Current code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const PASSWORD = "PASSWORD"
    Dim Row As Long
    If Target.Column = 4 Then
        Target.Parent.Unprotect PASSWORD
        Row = Target.Row
        Target.Parent.Range("A" & Row & ":" & "D" & Row).Locked = True
        Target.Parent.Protect PASSWORD
    End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You could use something like:

VBA Code:
With Me
    .Unprotect PASSWORD
    .Range(.Cells(Target.Row, 1), .Cells(Target.Row, 4)).Locked = True
    .Range(.Cells(Target.Row + 1, 1), .Cells(Target.Row + 1, 4)).Locked = False
    .Protect PASSWORD
End With
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 4 Then
      Me.Unprotect "Password"
      With Target.Offset(, -3).Resize(, 4)
         .Locked = True
         .Offset(1).Locked = False
      End With
      Me.Protect "Password"
   End If
End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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