"IF" with "AND" and "OR"

R

Richard Freist

I need an "IF" function that uses "AND" and "OR" in a nested function. Cell
A1 contains employee status "FT" or "PT" and B1 contains #of years employed.
"FT" employees get 5 days leave if employed 1 year or more, otherwise they
get 3 days. "PT" employees get 3 days leave if employed 2 years or more. I
have triied to work it out but can't get it. Can anybody show me how to build
this formula? I'm a student and this type of formula is required in my text
book.
Thanks...........
 
T

T. Valko

Have you tried any of the suggestions provided in your other 2 posts?

If those suggestions don't work how about responding to those who took the
time to offer you help and let them know rather than starting a new thread
multiple times.

Biff
 
J

JMB

You have several good replies at your other posts. If you must have "AND"
and "OR" nested in "IF", perhaps

=IF(AND(A1="FT",B1>=1),5,IF(OR(AND(A1="FT",B1<1),AND(A1="PT",B1>=2)),3,0))

but it seems less efficient than what has already been suggested.
 
E

Earl Kiosterud

Richard,

Someone will come up with a formula full of nested AND'S and OR'S, or at least, a mess of
nested IF's. You'll get dizzy looking at it. I think a table approach is more maintainable
and expandable. If this is supposed to be an exercise in IF AND and OR logic, it's not a
very good one -- such solutions are messy and not easy to maintain (change when status codes
are changed or added, years and days leave changed, etc) . Submit this anyway and get some
extra credit for what in this case would be an "out of the box" solution. Some
instructors hate that.

Make this table in K2:L5

FT0 3
FT1 5
PT0
PT2 3


You need to put in the days a PT gets with less than 2 years in the PT0 entry -- you didn't
say. Now if the Status is in Column A, and the Years is in Column B, use this formula to
get the days leave:

=VLOOKUP(A2&B2,$K$2:$L$5,2)

If you add any status codes or years, keep the whole table in sequence, and expand the range
of the formulas. Inserting the cells with a shift-down will take care of that
automatically. Once working, you might want to hide the table, or move (Cut/Paste) it
somewhere else, to some hidden columns, or even to another sheet. The formulas will adjust.
 
R

Richard Freist

Gary; thnk you for responding to my question, I am learning Excel and am
having a tough time of it with complex formulas. I will check out your
reference to the web site. This is my first time using this site and I guess
I didn't make my question clear enough. Thanks, Richard
 
R

Richard Freist

JMB; Thank you for taking the time to respond to my question, your formula
worked perfectly, I don't understand how you did it as I am still learning
Excel. My book, "Thompson New Perspectives, Excel 2003" in the appendix "A"
pg. A17- A18 doesn't explain how to create the formula. again thank you for
the information. Richard
 
R

Richard Freist

Biff; thank you for your comment, I am taking a class at Bellevue Community
College in Bellevue , WA. and the nested IF with the AND and OR function was
required in my text book. I wasn't clear enough on my first post about what I
needed and this was the first time I ever used this site. I appreciate your
comments and again , thank you very much. Richard
 
R

Richard Freist

Earl; thank you for taking the time to respond to my question. I'm sorry I
wasn't clear enough on my first post and I am new to this site and you are
right about getting dizzy looking at the formula I recieved from "JMB" but
that was what I was required to come up with. The text book didn't really
explain how to create the formula and I don't really like the book but that
is all I have to work with. I'll keep your ideas for future reference and I
hope I'll be able to use them in the future. Thanks again, Richard
 
J

joeu2004

I need an "IF" function that uses "AND" and "OR" in a nested function. Cell
A1 contains employee status "FT" or "PT" and B1 contains #of years employed.
"FT" employees get 5 days leave if employed 1 year or more, otherwise they
get 3 days. "PT" employees get 3 days leave if employed 2 years or more. I
have triied to work it out but can't get it. Can anybody show me how to build
this formula? I'm a student and this type of formula is required in my text
book.

Okay, you said this is for a class, and you __must__ use AND() and
OR() inside the IF(). I should reiterate that the following is
__not__ necessarily the best way to formulate the answer in real
life. But I think it meets the requirements of your class assignment.

=if(or(and(A1="FT",B1<1),and(A1="PT",B1>=2), 3, if(and(F1="FT",B1>=1),
5, ""))

The last AND() condition is over-specified. It would be sufficient to
write if(A1="FT",5,""). I am just trying to use AND() as much as
possible ;-).
 
J

joeu2004

I don't understand how you did it as I am still learning Excel.
My book, "Thompson New Perspectives, Excel 2003" in the appendix "A"
pg. A17- A18 doesn't explain how to create the formula.

The first thing to realize is: AND(), OR() and IF() are merely
functions that return values. You can use any function (of the
correct type) any place that you can use an expression or a constant
in an expression.

The IF() function takes 2 or 3 parameters of the form
if(expr1,expr2,expr3). "Expr1" is the condition used to select
"expr2" or "expr3". "Expr2" is the value used if "expr1" is true.
"Expr3" is the value used if "expr1" is false.

The AND() function takes 1 or more parameters of the form
and(expr1,expr2,...). Use AND() when you want to know if __all__ of
the parameters are true at the same time.

The OR() function takes 1 or more parameters of the form
or(expr1,expr2,...). Use OR() when you want to know if at least
__one__ of the parameters is true.

In your case, you wanted the result to be 5 when __one__ of the
following is true: (1) the status is "FT" __and__ years are 1 or
more; __or__ (2) the status is "PT" __and__ years are 2 or more.

The structure of the English description suggests on way to formulate
the nested test, namely: (1) and(status="FT",years>=1); __or__ (2)
and(status="PT",years>=2). From that, we get:
or(and(status="FT",years>=1), and(status="PT",years>=2)).

It is impossible to tell you how to formulate every possible problem.
You have to understand the basic concepts -- namely that functions can
be used anywhere an expression can be used -- and use those concepts
as building blocks.

PS: Although the book's intent is probably to demonstrate this
building-block concept, it is useful to also understand why you should
minimize the number of "building blocks" as much as possible. First,
nesting functions is often error-prone. You may be more likely to
create an obscure error in formulation, simply by misplacing a
parenthesis. Second, Excel 2003 and earlier version have a limit of 7
__nested__ functions. Learning to simplify logic as much as possible
may minimize the function depth.

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top