Lock rows of cells in Excel based on cell value

McM_

New Member
Joined
Oct 23, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi,
with this code I block the row if something is written in A.
For example, A3="AAA", the row "B3:M3" is blocked for editing.
Please help me with the modification of the code so that I can block the row "B3:M3" but also the other rows down from B3:M3.

If A9>"", then block B9:M9 down.

If A20>"", then block B20:M20 down, and so on.

Thank you very much!

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("B:M")) Is Nothing Then
 If UCase(Cells(Target.Row, "A")) > "" Then
 Application.EnableEvents = False
 MsgBox " blocked for editing !!!"
 Cells(Target.Row, "E").Select
 Application.EnableEvents = True
 End If
 End If
 
 End Sub

lock.xlsm
ABCDEFGHIJKLMN
1
2
3AAA
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Foaie1
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You can try something like this...

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'lock B-M if anything is entered in A.
If Not Intersect(Target, Range("A:A")) Is Nothing Then
ActiveSheet.Unprotect Password:="password here"
    If Target.Value = "" Then
        Range("B" & Target.Row & ":M" & Target.Row).Locked = False
    Else
        Range("B" & Target.Row & ":M" & Target.Row).Locked = True
    End If
ActiveSheet.Protect Password:="password here"
End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,220
Messages
6,123,694
Members
449,117
Latest member
Aaagu

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