Prevent data entry and set value to zero dependent on value of a different cell

%3CLINGO-SUB%20id%3D%22lingo-sub-2202447%22%20slang%3D%22en-US%22%3EPrevent%20data%20entry%20and%20set%20value%20to%20zero%20dependent%20on%20value%20of%20a%20different%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2202447%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3EI%20want%20to%20put%20some%20restrictions%20on%20a%20cell%20but%20I'm%20unsure%20how%20to%20do%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20word%20%22Yes%22%20has%20been%20typed%20into%20cell%20B2%2C%20i%20want%20to%20allow%20the%20user%20to%20input%20any%20numerical%20value%20in%20cell%20B5.%20However%20if%20the%20word%20%22No%22%20has%20been%20typed%20into%20cell%20B2%2C%20I%20want%20to%20prevent%20the%20user%20from%20entering%20any%20value%20to%20cell%20B5%20and%20also%20set%20the%20value%20of%20B5%20to%20zero.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20struggling%20a%20little%20to%20work%20out%20how%20to%20merge%20the%20commands%20together%20so%20if%20anyone%20has%20any%20ideas%20they%20can%20offer%2C%20they%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2202447%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hi there,

I want to put some restrictions on a cell but I'm unsure how to do it.

 

If the word "Yes" has been typed into cell B2, i want to allow the user to input any numerical value in cell B5. However if the word "No" has been typed into cell B2, I want to prevent the user from entering any value to cell B5 and also set the value of B5 to zero.

 

I'm struggling a little to work out how to merge the commands together so if anyone has any ideas they can offer, they would be greatly appreciated.

 

Many thanks

2 Replies

@nrog5 

This requires several steps.

 

1) Set up data validation for cell B5 to allow whole numbers or decimal numbers; specify a wide enough range.

 

2) Select all cells that the user should be able to edit, including B2 and B5.

Press Ctrl+1 to activate the Format Cells dialog.

Activate the Protection tab.

Clear the Locked check box.

Click OK.

 

3) On the Review tab of the ribbon, click Protect Sheet.

Use the check boxes to specify what users will be allowed to do. At the very least, leave the check box "Select unlocked cells" ticked.

If you wish, specify a password that will be needed to unprotect the sheet. Do not forget it!

Click OK. If you entered a password, you will have to re-enter it as confirmation.

In the step below, I will use "secret" as password.

 

4) Right-click the sheet tab and select "View Code" from the context menu.

Copy the following code into the worksheet module:

 

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("B2"), Target) Is Nothing Then
        Me.Unprotect Password:="secret"
        Application.EnableEvents = False
        Select Case Range("B2").Value
            Case "Yes"
                Range("B5").Locked = False
            Case Else
                Range("B5").Locked = True
                Range("B5").Value = 0
        End Select
        Application.EnableEvents = True
        Me.Protect Password:="secret"
    End If
End Sub

5) Switch back to Excel.

Save the workbook as a macro-enabled workbook (.xlsm).

Make sure that users allow macros when they open it.

@Hans Vogelaar Wow amazing, I'll give that a try. Thanks so much for your quick reply