woodworks wrote:=IF(B3>0<4,"1T",IF(B3>4<7,"2T",IF(B3>6<10,"3T",IF(B3>9<13,"4T"))))
woodworks wrote:[Why does my formula give] 1T as return no matter what number is in B3?
The conditional expression in the first IF function is B3>0<4. It has two operators. The tutorial says that both are precedence 0, so they are evaluated left to right. B3>0 is always true because we are told that B3 is in the range 1 to 12. True has value 1 in Calc so the second operator evaluates 1<4. This is also true. Since the conditional expression is true for any of the specfied values in B3, the formula result is always 1T.
woodworks wrote:I would have preferred … a correction of my code.
Based on what you've written I'm going to make the simplifying assumption that "a list of numbers in the range of 1 to 12" means "a list of
natural numbers in the range of 1 to 12". The first conditional expression presumably is intended to determine if B3 is 1, 2, or 3. We don't need to check for anything less than 1 because we know the allowed range is 1 to 12. So the first expression can be B3<4. As others have noted, the case where B3 is 4 is not covered by your formula. I will assume the intent is to map the next three natural numbers (4, 5, and 6) to 2T. We already know B4 is not 1, 2, or 3 since the second expression is in the
OtherwiseValue part of the first IF function. So the second conditional expression should be B3<7. Similarly the third conditional expression will be B3<10. If B3 fails all three tests, we know it is 10, 11, or 12, so there is no need for a fourth IF function. The complete formula for OpenOffice is
=IF(B3<4;"1T";IF(B3<7;"2T";IF(B3<10;"3T";"4T"))), as shown in the first sheet of the attachment.
But using nested IF functions quickly gets unwieldy. Suppose instead of 1T through 4T we have 1T though 40T. You won't want 39 nested IF functions even if Calc would accept that mess. The second sheet shows how to use a single VLOOKUP function and a table (in cells X1:Y4) to make the assignments. The formula is
=VLOOKUP(B3;$X$1:$Y$4;2) and it can be extended to 40T by just making more table entries with a minor change to the formula.
=VLOOKUP(B3;$X$1:$Y$40;2)
The third sheet shows how to use an array constant instead of a table, though this techinque doesn't extend as easily to 40T. LibreOffice syntax may be different than what I use in OpenOffice. However the attachment should show you the correct syntax in either case.
=VLOOKUP(B3;{1;"1T"|4;"2T"|7;"3T"|10;"4T"};2)
In this particular case, the assignements follow a simple pattern: each T value corresponds to three sequential natural numbers and there are no gaps. The QUOTIENT function divides two numbers and discards the remainder to produce an integer. We can easily calculate the T values directly from the values in B3 as shown in the fourth sheet.
=QUOTIENT(B3+2;3)&"T" When B3 is 5, adding 2 gives 7, and dividing by 3 gives 2 remainder 1. Concatenating 2 and "T" gives the result 2T.
If this answered your question please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.5, iMac Intel. The locale for any menus or Calc formulas in my posts is English (USA).