Hi,
I tooke your formula and changed lines each time we had a new IF. It
looks as follows:
=IF(AND(OR(C5=V13,C5=V15,C5=V16),C8>C7,(C8-C7)*24>W10,(V2-C7)*24>=W13),W13,
IF(AND(C5=V14,(V2-C7)*24>=W14),W14,
IF(AND(OR(C5=V13,C5=V15,C5=V16),C7<V2,(V2-C7)*24<W13),(V2-C7)*24,
IF(AND(C5=V14,C7<V2,(V2-C7)*24<W14),(V2-C7)*24,
IF(AND(OR(C5=V13,C5=V15,C5=V16),C7>C8,OR(C8<V2,C8>W2)),W13,
IF(AND(C5=V14,C7>C8,OR(C8<V2,C8>W2)),W14,
IF(AND(C7>=W2,C5<>V9,C5<>V10,C5<>V11,C5<>V12,C5<>V18,C5<>V19,C5<>V14),W13,
0)))))))
From what we see, your various options return one of the following
three expressions:
W13,
W14,
(V2-C7)*24
When we want to return N values with IF we can do it with N-1 IF's.
IF(cond1,Result1,IF(cond2,Result2,IF(...)))
You can group all the conditions returning the same result with an OR.
Then you will need only two IF's for 3 possible outcomes.
Also consider the following two approaches:
IF(cond1,Result1,IF(cond2,Result2,IF(...)))
IF(cond1,IF(cond2, ...), IF(cond3,...))
The first case is a degenerate binary tree, which effectively is a
linear structure with depth N-1, hence it requires N-1 levels of
nesting. The second case is non-degenerate, hence it requires O(log(N))
levels of nesting. For the second you need a divide-and-conquer method,
where one common condition is tested and subconditions are left for the
remaining ones.
Another thing you can do is, since several conditions appear frequently
(e.g. OR(C5=V13,C5=V15,C5=V16)), you can define named formulas,
alhtough this will only simplify the appearance and will not, per se,
reduce levels of nesting.
But then again maybe Harlan will come up with one of his impressive
solutions to such problems.
HTH
Kostis Vezerides