Data validation based on value in another cell

chasidar

New Member
Joined
Jan 6, 2011
Messages
12
Hi, I'm using Excel365. I searched the forum and didn't find a solution to my question.

I have 2 columns with data validation. In column E I choose yes or no from a list. In column D I have to choose who signed a form from a list of three options.

I want to have an option that if I choose No in column E, then N/A is automatically entered into column D. If yes is chosen in column E, I want to choose a value from the list in column D.

Is this possible?

I tried entering this in my data validation: =IF($E$2="No", "N/A" ,Lists!$A$1:$A$3) but it doesn't enter N/A in the cell if I choose No.
 

Attachments

  • sample.JPG
    sample.JPG
    72 KB · Views: 29

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Data validate rules cannot enter a value into a cell. They can only validate the value entered by a user.

In column D, you can have a formula that says
VBA Code:
=IF($E2="No","N/A","")
But you will have to remove the data validation, put in the formula, then put the data validation back again. Otherwise the data validation will reject the results of the formula. However, when adding data validation, it does not validate whatever is already in the cell.

Also, if the user ignores the "N/A" and selects an item from the dropdown list, it's going to wipe out the formula, so it will never be N/A again.

The only other way to do this is with VBA.

$scratch.xlsm
DEFG
1Signed ByMedicineJeanine
2N/ANoKathy
3 IbuprofenBud
4 Morphine
5 Ketamine
6 
7 
8 
Sheet3
Cell Formulas
RangeFormula
D2:D8D2=IF(E2="No","N/A","")
Cells with Data Validation
CellAllowCriteria
D2:D8List=$G$1:$G$3
 
Upvote 0
Data validate rules cannot enter a value into a cell. They can only validate the value entered by a user.

In column D, you can have a formula that says
VBA Code:
=IF($E2="No","N/A","")
But you will have to remove the data validation, put in the formula, then put the data validation back again. Otherwise the data validation will reject the results of the formula. However, when adding data validation, it does not validate whatever is already in the cell.

Also, if the user ignores the "N/A" and selects an item from the dropdown list, it's going to wipe out the formula, so it will never be N/A again.

The only other way to do this is with VBA.

$scratch.xlsm
DEFG
1Signed ByMedicineJeanine
2N/ANoKathy
3 IbuprofenBud
4 Morphine
5 Ketamine
6 
7 
8 
Sheet3
Cell Formulas
RangeFormula
D2:D8D2=IF(E2="No","N/A","")
Cells with Data Validation
CellAllowCriteria
D2:D8List=$G$1:$G$3
Thanks. Can you explain how I do this with VBA?
 
Upvote 0
One possibility with VBA. Put the following code in the sheet code window (right-click the sheet tab name & select View Code - put it in the window that appears on the right of screen)
Assumes you have a sheet called "Lists" and that there are entries on that sheet in the range A1:A3. This code goes in the sheet module of the sheet with the columns D & E you described in your original post.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 And Target.Column = 5 Then
        On Error GoTo Escape
        Application.EnableEvents = False
        If Target.Value = "No" Then
            With Target.Offset(0, -1)
                .Validation.Delete
                .Value2 = "N/A"
            End With
        ElseIf Target.Value = "Yes" Then
            With Target.Offset(0, -1)
                .Value = ""
                With .Validation
                    .Delete
                    .Add Type:=xlValidateList, _
                    AlertStyle:=xlValidAlertStop, _
                    Formula1:="=Lists!$A$1:$A$3"
                End With
            .Select
            End With
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub

Book1
DE
1Signed ByYes or No
2N/ANo
3
Sheet1
Cells with Data Validation
CellAllowCriteria
E2:E3ListYes,No


Book1
DE
1Signed ByYes or No
2Option 1Yes
3
Sheet1
Cells with Data Validation
CellAllowCriteria
E2:E3ListYes,No
D2List=Lists!$A$1:$A$3
 
Upvote 0
Thanks. I get an argument not optional error. I've reviewed the code but I can't see why this error should be returned.
 
Upvote 0
As per Jeff's question, run the following code (error handling removed) and let us know what line the error occurs on. It would also help if you could share your file via Google Drive, Dropbox or similar file sharing platform.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 And Target.Column = 5 Then
        Application.EnableEvents = False
        If Target.Value = "No" Then
            With Target.Offset(0, -1)
                .Validation.Delete
                .Value2 = "N/A"
            End With
        ElseIf Target.Value = "Yes" Then
            With Target.Offset(0, -1)
                .Value = ""
                With .Validation
                    .Delete
                    .Add Type:=xlValidateList, _
                    AlertStyle:=xlValidAlertStop, _
                    Formula1:="=Lists!$A$1:$A$3"
                End With
            .Select
            End With
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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