VBA Lock Cells in Range

tabbytomo

New Member
Joined
Jun 23, 2016
Messages
11
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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,601
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,543
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,543
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Forum statistics

Threads
1,141,613
Messages
5,707,402
Members
421,508
Latest member
Jalayne

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
Top