lock/unlock cell based on value in other cell

Laurens1

New Member
Joined
Dec 9, 2016
Messages
31
Hi Guys,

I want to lock fields when a specific cell is empty and unlock it when that specific cell contains a value.

Example:
[TABLE="width: 338"]
<colgroup><col><col span="5"></colgroup><tbody>[TR]
[TD] [/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Fruit[/TD]
[TD]Banana[/TD]
[TD]Pear[/TD]
[TD]Orange[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Apple[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[TD]unlocked[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Banana[/TD]
[TD]unlocked[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Pear[/TD]
[TD]locked[/TD]
[TD]unlocked[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Orange[/TD]
[TD]locked[/TD]
[TD]locked[/TD]
[TD]unlocked[/TD]
[TD]locked[/TD]
[/TR]
</tbody>[/TABLE]

Columns B:E should always be locked. But when Cell A2 is Apple, I want to unlock the cell in the apple column (E) on the same row.

Is this possible, and if yes... How?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Welcome to the forum!

Since you're wanting to change the Locked status of cells pragmatically, I think you'll need some code. Let us know if you need help placing it.

Right click your sheet containing the list and select "View Code" Paste and close Visual Basic.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim cel As Range
Dim r&, c&      'last row & column variable
Dim fCol$, fRow$    'label in title row/column


r = Cells(Rows.Count, 1).End(xlUp).Row
c = Cells(1, Columns.Count).End(xlToLeft).Column


'execute when change event is in first column
If Not Intersect(Cells(1, c), Target) Is Nothing Then
    Set rng = Range([B2], Cells(r, c))
    
    For Each cel In rng.Cells
        fCol = Cells(cel.Row, 1)
        fRow = Cells(1, cel.Column)
        If fCol = fRow Then
            cel.Locked = False
        Else
            cel.Locked = True
        End If
    Next cel
End If
End Sub

The code will run when there is a value changed in column A.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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