HuiliHuang wrote:
I have the below formula that returns me an error "The formula you typed contains an error". Is there any way to resolve this? Thanks.
=IF(OR(CW8>0),CONCATENATE(IF(CU8=0,0,IF(CU8<91,1,IF(AND(CU8>=91,CU8<=180),2,
IF(AND(CU8>=180,CU8<=365),3,IF(AND(CU8>365,CU8<=730),4,
IF(AND(CU8>365,CU8<=730),5,6)))))),"R"),
IF(CU8=0,0,IF(CU8<91,1,IF(AND(CU8>=91,CU8<=180),2,IF(AND(CU8>=180,CU8<=365),3,
IF(AND(CU8>365,CU8<=730),4,5))))))
I am almost sorry to say that you can write that formula exactly as you intended (making some assumptions; see below) if you eliminate the unnecessary conditions. You could write:
=IF(CW8>0,IF(CU8=0,0,IF(CU8<91,1,IF(CU8<=180,2,IF(CU8<=365,3,IF(CU8<=730,4,5))))) & "R",
IF(CU8=0,0,IF(CU8<91,1,IF(CU8<=180,2,IF(CU8<=365,3,IF(CU8<=730,4,5))))))
Or somewhat better:
=IF(CU8=0,0,IF(CU8<91,1,IF(CU8<=180,2,IF(CU8<=365,3,IF(CU8<=730,4,5))))) & IF(CW8>0,"R","")
But note that the second formula is not exactly the same as the first formula. When CW8<=0, the first formula returns
numeric 0 through 5, whereas the second formula returns
text "0" through "5".
IMHO, that difference is a "good thing". I think it is better if the formula consistently returns text, since it must return text when "R" is appended. But that is really your decision to make.
Alternatively, you might be able to use the following formula, which is more concise as well as more efficient and easily extended if the need arises in the future:
=LOOKUP(CU8,{0,1,91,181,365,730},{0,1,2,3,4,5}) & IF(CW8>0,"R","")
However, that makes the additional assumption that CU8 is never negative. If could be negative, your original formula would return 5 or "5R", the same value that it returns when CU8>730. That seems odd, IMHO. But again, that is your decision to make.
Also, the LOOKUP formula might not behave exactly the same as your original formula if CU8 might be non-integer. For example, the LOOKUP formula returns 0 if CU8 is 0.9, whereas your original formula returns 1.
Does that matter? If it does, we might be able to fix the LOOKUP formula (a good idea) if you tell us the number of decimal places that you expect the value in CU8 to be accurate to.
Finally, note that your original formula contains a number of ambiguities which I resolved my making some additional assumptions. If my assumptions are incorrect, please let us know how we should resolve them.
- You wrote: IF(AND(CU8>=91,CU8<=180),2,IF(AND(CU8>=180,CU8<=365),3. When CU8=180, it is unclear whether you want 2 or 3.
Since you wrote CU8>365 in the next term, I assume you meant CU8>180 here.
- You wrote: IF(AND(CU8>365,CU8<=730),4,IF(AND(CU8>365,CU8<=730),5,6. Note that the conditions are the same in both parts. So when CU8>365 and CU8<=730, it is unclear whether you want 4 or 5.
Since you wrote IF(AND(CU8>365,CU8<=730),4,5 in the part that applies when CW8<=0, I assume that is what you really meant in the part the first part.
However, if you truly mean to return 6 when CU8>730 and CW8>0 and only 5 when CU8>730 and CW8<=0, please let us know.