简体   繁体   中英

How can I remove an item from a cell's validation list when the cell already contains a value on that list?

I would like to remove an item from a cell's validation list when the cell already contains an item that has been selected from that validation list, in order to prevent the same item being re-selected in that cell.

Suppose an empty cell has the validation list {10,20,30,40,50}. When I select, say, '20' and then go back into that cell's validation list, I want to just see {10,30,40,50}. If I then select, say, '40' and go back into the validation list again, I want to see {10,20,30,50}, etc. It's important this also works for text values.

I originally viewed this problem as being about creating 2 lists and joining them together; eg in my first example, removing '20' can be thought of as creating lists {10} and {30,40,50}, so I tried using the 'comma' operator to join two ranges in the Data Validation dialogue, without success. I tried the same approach in a named formula and using that in the DV dialogue. Same result.

I created a UDF:

Function UNION(rRng1 As Range, rRng2 As Range) As Range
    Dim rRng12 As Range
    Set rRng12 = Union(rRng1, rRng2)
    UNION = rRng12
End Function

and tried this in both the DV dialogue and via a named range. Still didn't work.

[EDIT]: This functionality needs to work in a structured table, where one of the columns is updated from the validation list.

I've concluded that I need to use an array formula within a named range, and reference this from the DV dialogue, but I'm not clear on how this can be done?

Here's what I came up with. It will work for numbers, but will probably need some work if your list is text.

In E1:E5, put your list (10,20,30,40,50). In G1:G5, put the formula

{=SMALL(IF($E$1:$E$5*($E$1:$E$5<>$A$1),$E$1:$E$5,""),ROW())}

That takes the list from E, removes the number in A1 that matches, and sorts it. Now create a named range called dvExclude with this as the RefersTo

=Sheet1!$G$1:INDEX(Sheet1!$G$1:$G$5,COUNT(Sheet1!$G$1:$G$5))

The G range will have errors at the bottom and this will exclude those - COUNT only counts numbers, not errors.

Finally, set up your data validation in A1 as List and =dvExclude.

数据验证(不包括所选值)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM