SWITCH function
The SWITCH function in Excel is a logical function that evaluates an expression against a list of values and returns the corresponding result for the first matching value. It is a simpler and more efficient alternative to using multiple IF statements, especially when you have several conditions to check.
Syntax:
=SWITCH(expression, value1, result1, [value2, result2], ..., [default])
- expression: The value or expression to evaluate.
- value1, value2, …: The values that the expression will be compared against.
- result1, result2, …: The results returned for each corresponding value.
- [default] (optional): The value to return if none of the values match the expression. If omitted, and no match is found, it will return an error.
Example 1: Basic Usage
If you have a number in cell A1, and you want to return the corresponding grade based on the value (e.g., 1 = “A”, 2 = “B”, etc.), you can use:
=SWITCH(A1, 1, "A", 2, "B", 3, "C", "Invalid")
- If
A1is1, it will return"A". - If
A1is2, it will return"B". - If
A1is3, it will return"C". - If
A1contains a value other than 1, 2, or 3, it will return"Invalid".
Example 2: Using with Text Values
You can also use SWITCH to evaluate text values. For instance, if cell A1 contains a day of the week (e.g., “Monday”, “Tuesday”, etc.), you could use:
=SWITCH(A1, "Monday", "Start of the week", "Friday", "Almost weekend", "Weekend", "Enjoy")
- If
A1is “Monday”, it will return"Start of the week". - If
A1is “Friday”, it will return"Almost weekend". - If
A1is “Weekend”, it will return"Enjoy". - If
A1contains any other value, it will return an error.
Example 3: Default Result
You can include a default result if none of the values match the expression. For example:
=SWITCH(A1, 1, "First", 2, "Second", 3, "Third", "Invalid Number")
- If
A1is 1, it returns"First". - If
A1is 2, it returns"Second". - If
A1is 3, it returns"Third". - If
A1is any other value, it returns"Invalid Number".
Key Points:
- The
SWITCHfunction evaluates an expression once and compares it with multiple values, simplifying the structure of your formula compared to nestedIFstatements. - If the expression matches one of the specified values, the corresponding result is returned.
- If no match is found and a default is specified, the default result is returned. If no default is provided, an error will occur.
Benefits:
- Cleaner syntax:
SWITCHis more concise and easier to read than multiple nestedIFstatements. - More efficient: It evaluates the expression only once, rather than re-evaluating it for each
IFcondition. - Flexible: You can use
SWITCHwith numeric values, text, or logical expressions.
The SWITCH function is especially useful when dealing with multiple possible outcomes based on a single expression, such as mapping numbers or text to corresponding categories or values.