VBA Function: Switch

The VBA Switch function returns the value corresponding to the first expression that evaluates to True (or returns the Null value if no match is found).

It is a shortened version of the Select Case statement.

Usage:

Switch(test_1, value_1, test_2, value_2, etc.)


Example of Usage

The courseURL function here returns the URL corresponding to the given course using the Select Case statement:

Function courseURL(course)

    Select Case course
        Case Is = "Excel"
            courseURL = "https://www.excel-pratique.com/en/excel-training"
        Case Is = "VBA"
            courseURL = "https://www.excel-pratique.com/en/vba"
        Case Is = "Sheets"
            courseURL = "https://www.sheets-pratique.com/en/course"
        Case Is = "Apps Script"
            courseURL = "https://www.sheets-pratique.com/en/apps-script"
    End Select
        
End Function

Sub example()

    MsgBox courseURL("VBA") 'Returns: https://www.excel-pratique.com/en/vba
    
End Sub

The courseURL function returns the URL corresponding to the given course using the Switch function:

Function courseURL(course)

    courseURL = Switch( _
        course = "Excel", "https://www.excel-pratique.com/en/excel-training", _
        course = "VBA", "https://www.excel-pratique.com/en/vba", _
        course = "Sheets", "https://www.sheets-pratique.com/en/course", _
        course = "Apps Script", "https://www.sheets-pratique.com/en/apps-script")
        
End Function

Sub example()

    MsgBox courseURL("VBA") 'Returns: https://www.excel-pratique.com/en/vba
    
End Sub