How to use Excel Switch function to compare with range of values
I would like to use the SWITCH function to detect if a number is between two other numbers, if not test the next range of numbers and so on.
The objective is to have a one cell answer and then copy the formula over a column (of dates in this example).
I tried this :
=SWITCH(MONTH(44562);{1;2;3};"T1";{4;5;6};"T2";{7;8;9};"T3";{10;11;12};"T4")
But the function spills for each element of the array and the comparison is done by level of the array :
I could work around this by using an IFS function but that would require to write (and evaluate) the logical test for each pair of argument and that is what I would like to evade.
Maybe I am tackling this issue from the wrong angle but I would still like to know if this usage of SWITCH is somehow possible. Thank you in advance.
The answerS :
The one from Reddy Lutonadio is very flexible for list of distinct values. It is the one I accepted.
The one from Ron Rosenfeld is very compact, efficient, can work with real numbers but the lists must have the same length.
My opinion : Use IFS for more complex situations (and maybe add the LET function in the mix).
Solution 1:
Simpler may not be flexible enough for your needs. But to return the same result, using SWITCH
:
=SWITCH(INT((MONTH(A1)-1)/3)+1,1,"T1",2,"T2",3,"T3",4,"T4")
You can obtain the same result with a simpler formulas:
=TEXT(INT((MONTH(A1)-1)/3)+1,"\T0")
=CHOOSE(INT((MONTH(A1)-1)/3)+1,"T1","T2","T3","T4")