Boolean Operators Missing From Cell Expressions In Excel?
(how's that for a geeky title) Okay, the simple excel function i wanted to teach someone to write: "If A1 is between 1986 and 2000 (inclusively) then print '1', otherwise print '0' " How would you write that as a cell function?
I haven't done much excel lately. Here's how I thought it would be written:
=IF(B12 >=1986 AND B12 <= 2000,1,0) <-- Nope!!!
How about:
=IF(B12 >=1986 && B12 <= 2000,1,0) <-- Uh-uh!! No siree!!
(i was still refusing to look up help at this point...)
Forget it... i'll nest the logic...
=IF(B12 >=1986, IF(B12 <= 2000), 1 ,0) ,0) <-- Yes this worked. But...
Yuck. I mean... honestly!!
So I tried...
=IF(B12 BETWEEN (1986, 2000), 1, 0) <-- Doesn't work
=IF(B12 IN (1986...2000), 1, 0) <-- Doesn't work
(also tried with square brackets, curly brackets, angle brackets, pipe symbols, the phoenician alphabet, a donkey's leg, a bat's wing....)
Looked in help... and oh my god how lispy is this:
=IF(AND(B12 >=1986, B12 <= 2000), 1, 0)
How am i supposed to teach my frail elderly aunt to grok that?
It's elegant from the computer's point of view... it's an expression tree... i'd enjoy writing a program to parse that, i know i would... but teaching a regular human to read it, use it, debug it, maintain it, trust it, tweak it, share it, co-opt it, .... never.
This is a symptom of excel decimating the competition a long time ago and then stagnating. Yes: the dodo effect.
If excel were a version 2 product, I'd say "hey that's pretty good", but we're talking about version 11 here. They should've allowed cell functions like this: "= IF (B12 >=1986 AND B12 <= 2000, 1, 0) " long long ago. Operators... operators... not just functions. Lisp had it back in 1902. ah forget it. I wonder how wikiCalc handles cell functions? or spreadsheets.google.com?
'Daniel Bowen' on Thu, 31 Aug 2006 05:11:27 GMT, sez: =IF(ABS(B12-1993)<=7,1,0) :-)
'lb' on Thu, 31 Aug 2006 05:44:20 GMT, sez: top answer Dan!
'Kalpesh' on Thu, 31 Aug 2006 09:23:41 GMT, sez: It seems excel has a different way of "AND"ing the conditions
=IF(AND(A1>=1986, A1<=2000), 1, 0)
'Jody' on Thu, 31 Aug 2006 09:43:20 GMT, sez: Daniel's solution is elegent; however it isn't obvious from looking at it that it does the logic you asked; what if you wanted to change the years? You would have to do a bit of math in order to update the formula.
Here one solution:
=IF(A1 >= 1986, IF(A1<=2000,1,0), 0)
A user looking at that formula can easily see what values needs to be changed.
'Simon' on Fri, 01 Sep 2006 14:19:31 GMT, sez: Excel doesn't have any operators. All it has is *functions* that are named like operators. Once you figure that out, it will make sense AND(StopTryingInExcel)
'googlexcel' on Sat, 02 Sep 2006 23:17:20 GMT, sez: google spreadsheet does it the same way...
'Nick Hodge' on Sun, 03 Sep 2006 23:25:45 GMT, sez: Is A1 a date?
Oh my gawd I hope not, otherwise your poor elderly aunt is going to end up in a loony bin
'Calybute' on Sun, 09 Sep 2007 07:01:46 GMT, sez: Try this...
=IF(A1<1986,0,IF(A1<=2000,1,IF(A1>2000,0)
It works and is it simpler than IF(AND)s to understand...
'Calybute' on Sun, 09 Sep 2007 07:03:43 GMT, sez: Actually, that is essentially Jody's, written in longhand... but I found it easier to read....
'greg' on Wed, 28 Oct 2009 11:56:57 GMT, sez: Excel will evaluate the function to boolean.
=AND(A1>=1986,A1<=2000)
To coerce the TRUE/FALSE to 1,0 just multiply by 1 or use the double unary --.
=--AND(A1>=1986,A1<=2000)
=1*AND(A1>=1986,A1<=2000)
'Brian Minton' on Thu, 10 Jun 2010 22:52:45 GMT, sez: Excel does not have boolean operators, but it does have *arithmetic* operators. Excel uses the convention 0 = FALSE, any non-zero number = true, so you can do something like this:
=IF((B12 >=1986) * (B12 <= 2000),"yes","no")
This is not exactly intuitive though, and in your particular example, since you only want a 1 or a 0, the if statement is not even strictly necessary, as you could do the following:
=(B12 >=1986) * (B12 <= 2000)
|