Lock row based on value in first column

Elraw

New Member
Joined
Aug 31, 2018
Messages
5
Hello everyone,
I'm struggling to get this code right. Hopefully someone here can steer me in the right direction and help me out.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]LOCK[/TD]
[TD]PERSON[/TD]
[TD]DATE[/TD]
[TD]CONTROL no[/TD]
[TD]Value 1[/TD]
[TD]Value 2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 1[/TD]
[TD]01-01-2018[/TD]
[TD]1234[/TD]
[TD]15[/TD]
[TD]55[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 2[/TD]
[TD]20-02-2018[/TD]
[TD]5678[/TD]
[TD]20[/TD]
[TD]42[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 3[/TD]
[TD]15-04-2018[/TD]
[TD]9012[/TD]
[TD]34[/TD]
[TD]37[/TD]
[/TR]
</tbody>[/TABLE]

Above is a sample of what I'm trying to accomplish, possibly by VBA.
The sheet I'm working with is a password protected sheet, with only the cells unlocked that may have data entered into them.
However, I want the data in the rows to be locked after the first cell in the row (LOCK row) has value YES.
If the data in cell A2 is YES, data in cells B2:F2 cannot be edited anymore. If the data in A2 is NO, editing is possible again.
First time I tried this with Data Validation, but the problem there was the data could not be changed, however the content could be cleared.

I've found some code that worked, but this only works on 1 cell or row. I need it to work for the full sheet, each row independently.
As the sheet might content hundreds of rows, of course I cannot just copy paste the code into oblivion.
The VBA code is tried was as below:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect ""
    If Range("A2") = "NO" Then
        Range("B2:F2").Locked = False
    ElseIf Range("A2") = "YES" Then
        Range("B2:F2").Locked = True
    End If
    ActiveSheet.Protect ""
End Sub

Hope everything is clear, and someone can help me out here.
Thanks in advance.

Kind regards,
Elraw
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hello everyone,
I'm struggling to get this code right. Hopefully someone here can steer me in the right direction and help me out.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]LOCK[/TD]
[TD]PERSON[/TD]
[TD]DATE[/TD]
[TD]CONTROL no[/TD]
[TD]Value 1[/TD]
[TD]Value 2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 1[/TD]
[TD]01-01-2018[/TD]
[TD]1234[/TD]
[TD]15[/TD]
[TD]55[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 2[/TD]
[TD]20-02-2018[/TD]
[TD]5678[/TD]
[TD]20[/TD]
[TD]42[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Person 3[/TD]
[TD]15-04-2018[/TD]
[TD]9012[/TD]
[TD]34[/TD]
[TD]37[/TD]
[/TR]
</tbody>[/TABLE]

Above is a sample of what I'm trying to accomplish, possibly by VBA.
The sheet I'm working with is a password protected sheet, with only the cells unlocked that may have data entered into them.
However, I want the data in the rows to be locked after the first cell in the row (LOCK row) has value YES.
If the data in cell A2 is YES, data in cells B2:F2 cannot be edited anymore. If the data in A2 is NO, editing is possible again.
First time I tried this with Data Validation, but the problem there was the data could not be changed, however the content could be cleared.

I've found some code that worked, but this only works on 1 cell or row. I need it to work for the full sheet, each row independently.
As the sheet might content hundreds of rows, of course I cannot just copy paste the code into oblivion.
The VBA code is tried was as below:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect ""
    If Range("A2") = "NO" Then
        Range("B2:F2").Locked = False
    ElseIf Range("A2") = "YES" Then
        Range("B2:F2").Locked = True
    End If
    ActiveSheet.Protect ""
End Sub

Hope everything is clear, and someone can help me out here.
Thanks in advance.

Kind regards,
Elraw

Try using
Code:
[COLOR=#333333] Row("2:2").Locked = True
[/COLOR]
 
Upvote 0
Thanks for responding nemmi69.
I just tried to change it, but it gives me a Compile error (Sub or Function is not defined).
Full code I tried - I am zero VBA experience so I might be doing it wrong.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect ""
    If Range("A2") = "NO" Then
        Row("2:2").Locked = False
    ElseIf Range("A2") = "YES" Then
        Row("2:2").Locked = True
    End If
    ActiveSheet.Protect ""
End Sub

However, looking from a logical point of view this doesn't work either.
As I tried to explain, I don't need it JUST for row 2, but for the entire sheet.
I.e. if Cell A2 contains YES, block B2:F2. If Cell A3 contains YES, block B3:F3, if Cell A4 contains YES, block B4:F4, etc. etc.

Hope it's clear enough now.
Appreciate your time and help.
 
Upvote 0
Sorry miss an 's'

Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect ""
If Range("A2") = "NO" Then
Rows("2:2").Locked = False
ElseIf Range("A2") = "YES" Then
Rows("2:2").Locked = True
End If
ActiveSheet.Protect ""
End Sub
 
Upvote 0
Thanks. This works, however not the way I want it to work.
Column A should always be editable. With this code it locks the entire Row 2, so also the Cell that should be editable (A2).

Also, this code still doesn't work as I want it to work, namely to have each row individually be locked by the first cell in the column.

Anyone might have an idea to how this is possible?
 
Upvote 0
How about

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim UserRow As Long
'assuming your range is rows 2 to 20
UserRow = Target.Row
If UserRow >= 2 And UserRow <= 20 Then
    ActiveSheet.Unprotect ""
    If UCase(Range("A" & UserRow).Value) = "NO" Then
        Range("B" & UserRow & ":XFD" & UserRow).Locked = False
    ElseIf UCase(Range("A" & UserRow).Value) = "YES" Then
        Range("B" & UserRow & ":XFD" & UserRow).Locked = True
    End If
    ActiveSheet.Protect ""
Else
    MsgBox "Row " & UserRow & " is outside range"
End If
End Sub
 
Upvote 0
Thanks a lot nemmi69!
This seems to work really good.

The number of rows won't be unlimited. But say I want this code to work on the first 500 rows, I simply have to change the 5th line as follows?
Code:
If UserRow >= 2 And UserRow <= 500 Then

Thanks in advance again.
 
Upvote 0
That's it and to restrict it to column A

Code:
Dim UserRow As Long
'assuming your range is rows 2 to 500 and only for column 1 (A)
UserRow = Target.Row
If UserRow >= 2 And UserRow <= 500 And Target.Column = 1 Then
 
Upvote 0
I just tested it, and it seems to work good.
Only problem I have now is, that whatever I type in columns B and further, I get the error "Row X is outside range" (where X is the row number I'm currently on).

Value in Column A = YES => I cannot change anything, as supposed to so this is working perfect
Value in Column A = NO => I can edit and type just fine, only thing is I keep getting the error message. It's just a message by the way, it doesn't restrict me from actual editing. This is working fine.

What should I change to get rid of this message? Or just remove that complete line
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim UserRow As Long
'assuming your range is rows 2 to 500 and only for column 1 (A)
UserRow = Target.Row
If UserRow >= 2 And UserRow <= 500 And Target.Column = 1 Then

    ActiveSheet.Unprotect ""
    If UCase(Range("A" & UserRow).Value) = "NO" Then
        Range("B" & UserRow & ":XFD" & UserRow).Locked = False
    ElseIf UCase(Range("A" & UserRow).Value) = "YES" Then
        Range("B" & UserRow & ":XFD" & UserRow).Locked = True
    End If
    ActiveSheet.Protect ""
End If
End Sub
 
Upvote 0
Remove
Code:
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]    MsgBox "Row " & UserRow & " is outside range"[/COLOR]

This was just used to show me anything outside the desired range.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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