简体   繁体   中英

How to use data validation in Excel to ensure only items from a list are entered into cell (multiple selection)

I have a list of numbers (units within our building) that I need users of the workbook to enter into a cell. I need to restrict users to entering either a single unit from the list, or multiple, in the following format:

[unit]

[unit], [unit], [unit] etc. Note here that the units are separated by a single comma then a space.

I am not able to write a macro for this as the file needs to stay in .xlsx format. So I need a formula to check whether the cell is in one of these formats. The image below shows what that formula would return if entered into column B.

在此处输入图片说明

So far I have tried:

=OR(ISNUMBER(FIND("101",A2)),OR(ISNUMBER(FIND("102",A2)),OR(ISNUMBER(FIND("103",A2)),OR(ISNUMBER(FIND("104",A2)),OR(ISNUMBER(FIND("201",A2)),OR(ISNUMBER(FIND("202",A2)),OR(ISNUMBER(FIND("203",A2)),OR(ISNUMBER(FIND("204",A2)),OR(ISNUMBER(FIND("205",A2)),OR(ISNUMBER(FIND("206",A2)),ISNUMBER(FIND(", 101",A2)),OR(ISNUMBER(FIND(", 102",A2)),OR(ISNUMBER(FIND(", 103",A2)),OR(ISNUMBER(FIND(", 104",A2)),OR(ISNUMBER(FIND(", 201",A2)),OR(ISNUMBER(FIND(", 202",A2)),OR(ISNUMBER(FIND(", 203",A2)),OR(ISNUMBER(FIND(", 204",A2)),OR(ISNUMBER(FIND(", 205",A2)),OR(ISNUMBER(FIND(", 206",A2)))))))))))))))))))))

This finds if there is at least one instance of any of the units, or units with a comma and a space in front, then returns TRUE. However, I can enter anything else into the cell and as long as there is at least one of these criteria, it will still return TRUE. I need the formula to return TRUE if the cell contains one (or more) of these terms, and nothing else.

Assuming that all unit numbers are three digits in length, as per your example, try the following solution. Note that this solution uses a helper column (which you can hide) to determine whether the entry is valid, where the formula returns TRUE or FALSE. Then, the Data Validation refers to this helper column. Unfortunately, Data Validation won't accept the formula.

Let's say that A1:E11 contains your sample data, and that Column B is used as a helper column. First, select cell B2. With this cell selected, define the following two names (Ribbon >> Formulas >> Defined Names >> Name Manager)...

First Defined Name

Name:  MyArray1

Refers to:  =MID($A2&", ",ROW(INDIRECT("1:"&LEN($A2)-LEN(SUBSTITUTE($A2,",",""))+1))*5-5+1,5)

Second Defined Name

Name:  MyArray2

Refers to:  =MID(SUBSTITUTE($A2," ",""),ROW(INDIRECT("1:"&LEN($A2)-LEN(SUBSTITUTE($A2,",",""))+1))*4-4+1,3)+0

Then, enter the following formula in B2, confirm with CONTROL+SHIFT+ENTER, and copy down:

=(AND(ISNUMBER(MATCH(MyArray1,$E$2:$E$11&", ",0)))*(NOT(OR(FREQUENCY(MyArray2,MyArray2)>1))))>0

Now you can set up your Data Validation. First, select cell A2. With this cell selected, click on Data Validation (Ribbon >> Data >> Data Tools >> Data Validation). Then, under Settings, click on the drop down arrow under Allow, and select Custom. Then, under Formula, enter the following formula...

=$B2

Then, copy the Data Validation to you other cells in Column A.

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