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:
ABCDE
1FruitBananaPearOrangeApple
2Applelockedlockedlockedunlocked
3Bananaunlockedlockedlockedlocked
4Pearlockedunlockedlockedlocked
5Orangelockedlockedunlockedlocked

<colgroup><col><col span="5"></colgroup><tbody>
</tbody>

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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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