Multiple values for an AND/OR statement

混江龙づ霸主 提交于 2019-12-11 03:05:10

问题


Is it possible to combine AND (or OR) statement criteria to avoid repeating a cell name?

For example, I want to see if cell C2 contains the any of the numbers 2,3,5,7, or 10. I want to avoid writing

IF(AND(C2=2,C2=3... etc. 

and simplify it to an array of the numbers like

IF(AND(C2=[2,3,5,7,10]... 

Unfortunately, I have a lot more than just 5 numbers to add so it's getting be very laborious. Anyone have an easier way than repeating the cell name=__ over and over?


回答1:


You can use an "array constant" like this

=IF(OR(C2={2,3,5,7,10}),"Yes","No")

.....or for a large set of numbers you could put all the numbers in a cell range, e.g. Z2:Z100 and do the same

=IF(OR(C2=$Z$2:$Z$100),"Yes","No")

although when you use a range rather than an array constant the formula becomes an "array formula" so needs to be confirmed with CTRL+SHIFT+ENTER

perhaps better to use COUNTIF and avoid "array entering"...

=IF(COUNTIF($Z$2:$Z$100,C2)>0,"Yes","No")




回答2:


=IF(ISERROR(MATCH(C2,{2,3,5,7,11},0)),"no","yes")

No need to enter this as an array formula. How it works: MATCH returns a #N/A! error if it can't find the lookup value in the lookup array. ISERROR catches this.

But as barry houdini suggests, you may want to put your numbers in some range e.g. Z1:Z5 instead of hard-coding them into your formula. So you would have this formula instead:

=IF(ISERROR(MATCH(C2,Z1:Z5,0)),"no","yes")



来源:https://stackoverflow.com/questions/29258010/multiple-values-for-an-and-or-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!