Table of contents
Collapse the table of content
Expand the table of content

Switch Function

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Evaluates a list of expressions and returns a Variant value or an expression associated with the first expression in the list that is True. SyntaxSwitch(expr-1,value-1 [ ,expr-2,value-2 [ ,expr-n,value-n ]] ) The Switch function syntax has these parts:

exprRequired. Variant expression you want to evaluate.
valueRequired. Value or expression to be returned if the corresponding expression is True.

Remarks The Switch functionargument list consists of pairs of expressions and values. The expressions are evaluated from left to right, and the value associated with the first expression to evaluate to True is returned. If the parts aren't properly paired, arun-time error occurs. For example, if expr-1 is True, Switch returns value-1. If expr-1 is False, but expr-2 is True, Switch returns value-2, and so on. Switch returns aNull value if:

  • None of the expressions is True.

  • The first True expression has a corresponding value that is Null.

Switch evaluates all of the expressions, even though it returns only one of them. For this reason, you should watch for undesirable side effects. For example, if the evaluation of any expression results in a division by zero error, an error occurs.


This example uses the Switch function to return the name of a language that matches the name of a city.

Function MatchUp(CityName As String)
    Matchup = Switch(CityName = "London", "English", CityName _
                    = "Rome", "Italian", CityName = "Paris", "French")
End Function
© 2017 Microsoft