Lock excel cell based on criteria

propolis

New Member
Joined
Mar 22, 2019
Messages
36
Hi,

Link to my spreadsheet: Spreadsheet

In my spreadsheet cell B2 is a Yes No option field

If you select Yes the Cell B3 is clear and you can enter data.

If you select NO, the Cell in B3 gets a grey colour, BUT, when they select No and the cell is Grey, I would like to protect the cell B3 so that you cant enter any text in cell B3

Hope this makes sense

Propolis

EDIT:
There are about 30 plus Yes / No selection per worksheet
 
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this using the follwong VBA:

When/if data is in C2:C31 when yes is selected, when 'No' is selected then that data is deleted, greyed out and locked.

There is no password on the locked cell, so the 'end user' can select yes/no at their leisure, I hope it helps!!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim cell As Range
Dim i As Integer

Set ws = ThisWorkbook.Sheets("Sheet1") ' Adjust the sheet name if needed

' Only respond to changes in column B
If Not Intersect(Target, ws.Range("B2:B31")) Is Nothing Then
Application.EnableEvents = False
ws.Unprotect

' Loop through the specified range
For i = 2 To 31
If ws.Cells(i, 2).Value = "No" Then
ws.Cells(i, 3).Value = "" ' Clear the cell content
ws.Cells(i, 3).Locked = True
Else
ws.Cells(i, 3).Locked = False
End If
Next i

ws.Protect
Application.EnableEvents = True
End If
End Sub

Book2.xlsm
ABCDE
1SelectInformation
2No
3No
4YesData
5No
6Select
7No
8Select
9Select
10Select
11No
12YesData
13No
14Select
15Select
16Select
17Select
18No
19Select
20Select
21Select
22Select
23YesData
24Select
25Select
26Select
27Select
28Select
29Select
30Select
31Select
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:C31Expression=$B2="No"textNO
 
Upvote 0
Hi,

Thanks for the code.

Works as I wanted it but have one issue:

When I select 'No' the cell gets protected but the whole worksheet as well. I have to unprotect the worksheet, do the next line, and if 'No' the whole worksheet gets protected.

Any suggestions please?

Propolis
 
Last edited:
Upvote 0
Hi,

Go to the "Review" tab and click "Protect Sheet".
Ensure that the "Select locked cells" and "Select unlocked cells" options are checked. You may set a password if desired (probably not in your case).
Make sure you uncheck the "Edit objects" and "Edit scenarios" options to allow the VBA code to make changes to the locked status of cells.

See if that helps.

Dj
 
Upvote 0
Hi there,

I have done some testing...

1. Ctrl+a (select the whole sheet)
2. Right Click anywhere, select 'Format Cells' (you may need to unlock sheet first)
3. Make sure that in the 'Protection tab' 'Locked and Hidden' are UNCHECKED

4. Try the sheet again...

1723103996186.png



Dj
 
Upvote 0
There are about 30 plus Yes / No selection per worksheet
Are you using Yes/No drop downs in column B? If you select Yes in B2, cell B3 is cleared and you can enter data. If you select No in B2, cell B3 is greyed out and locked. Is this pattern followed down column B? For example, if you select Yes in B3, cell B4 is cleared and you can enter data. If you select No in B3, cell B4 is greyed out and locked and so on down column B. Is this correct?
 
Upvote 0
Start by unlocking all the cells in your sheet. You can do this by clicking on the cell to the left of column letter A and above the row number 1 in the upper left hand corner. This will select all the cells in the sheet. Next right click anywhere on your sheet and click Format Cells. Then click the Protection tab and remove the check mark beside 'Locked'. Next copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Change the password (in red) to suit your needs. Close the code window to return to your sheet.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 2 Then Exit Sub
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect "MyPassword"
    Select Case Target.Value
        Case "Yes"
            With Target.Offset(1)
                .ClearContents
                .Select
            End With
        Case "No"
            With Target.Offset(1)
                .Interior.ColorIndex = 15
                .Locked = True
            End With
    End Select
    ActiveSheet.Protect "MyPassword"
    ActiveSheet.EnableSelection = xlUnlockedCells
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
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