CHOOSE |
CHOOSE(index, choice1 [,choice2] [,]) |
Returns the value from a list of values based on an index number (Variant). |
index | The index number (Integer). |
choice1 | The expression containing the first possible choice (Variant). |
choice2 | (Optional) The expression containing the second possible choice (Variant). |
REMARKS |
* The "index" can be any numeric expression. * If "index" = 1 then "choice1" is returned. * If "index" = 2 then "choice2" is returned. * If "index" < 1, then Null is returned. * If "index" > the number of choices, then Null is returned. * If "index" is not a whole number, then it is rounded to the nearest whole number. * All the choices have to be submitted as individual items, you cannot pass in an array. * Every choice is evaluated in the list, even though only one is returned. * This can be quickly used to return a selection without having to create an array. * You can use the IIF function to returns one of two parts, depending on the evaluation of an expression. * You can use the SWITCH function to return a value based on expressions. * The equivalent Excel function is Application.WorksheetFunction.Choose * The equivalent .NET function is Microsoft.VisualBasic.Interaction.Choose * For the Microsoft documentation refer to learn.microsoft.com |
Dim sReturn As String
sReturn = Choose(1, "one", "two", "three")
Debug.Print sReturn '= "one"
sReturn = Choose(2, "one", "two", "three")
Debug.Print sReturn '= "two"
sReturn = Choose(3, "one", "two", "three")
Debug.Print sReturn '= "three"
sReturn = Choose(5, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
Debug.Print sReturn '= 5
sReturn = Choose(5.9, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
Debug.Print sReturn '= 5
sReturn = Choose(5, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
Debug.Print sReturn '= 5
sReturn = Choose(40, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", _
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", _
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", _
"31", "32", "33", "34", "35", "36", "37", "38", "39", "40")
Debug.Print sReturn '= 40
sReturn = Choose(35, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
Debug.Print sReturn ' run-time error, only 10 elements
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top