简体   繁体   中英

Check if any values of one column contains any values from another column

So I have a formula I've been using to see if any of the cells in one column matches any of the cells in another group of values (in a different sheet called companies):

 =IF(ISERROR(VLOOKUP(K1,companies!A:A,1,FALSE)),"Keep","Delete")

Now I'm trying to do the exact same thing, only now I want to see if any of the cells CONTAIN any substring of the cells (companies!A:A) rather than MATCH. Not sure how to do this though.

=IF(ISERROR(VLOOKUP(CONCATENATE("*",K1,"*"),companies!A:A,1,FALSE)),"Keep","Delete")

Just change your K1 reference to CONCATENATE("*",K1,"*") which will make the vlookup wildcard based.

Here's an image showing this working, though modified to all be in one sheet. You can see one, four and five get delete because they're contained in the list, but not EXACTLY in the list.

EDIT So after some discussion in the comments, it turns out the above is a misunderstanding of the need. The question actually wants to be able to see if a certain value contains ANY of the strings in another column, which is a bit more difficult of a problem - but this was bugging me so I worked on it and I think I've got something that works.

Here's the formula:

=IF(OR(ISNUMBER(SEARCH("*"&$H$2:$H$14&"*",I2))),"Delete","Keep")

This has to be entered using CTRL-SHIFT-ENTER to make it an array function to work

The reference column must also be explicitly defined (ie H#:H#,not H:H) or else everything will return true, as the empty cells below your data will match anything.

That all being said, you'll know its an array function because it will have brackets around the formula in the formula bar as you can see below.

数组公式

I strongly recommend going through the "Evaluate Formula" tool to see how this works yourself, but I'll break it down best I can below, using an example of a matching row since it's more obvious.

First, excel evaluates your reference and concatenation into an array of the cell values and replaced your reference for the string to be searched, so we get something like:

=IF(OR(ISNUMBER(SEARCH({"*john*";"*jack*";"*fake*";"*bill*";"*bob*";"*test*";"*blah*";"*hey*";"*words*";"*ten*";"*eleven*";"*jim*";"*susie*"},"fake@example.org"))),"Delete","Keep")

Excel then performs the search for each of those terms against the string you want to search, and this returns an array of the results of that. Note search returns "#VALUE" when there's no match.

=IF(OR(ISNUMBER({#VALUE;#VALUE;1;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE;#VALUE})),"Delete","Keep")

Excel then does IsNumber against this array, and again, stores the True/False results in the array.

=IF(OR({FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE}),"Delete","Keep")

We then added the OR, which will evaluate against the array and return True if any of the results are True, and then the If is evaluated.

Seems to work, though performance may suck against a large dataset. This kind of stuff would be better suited for some sort of SQL tool.

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