Choose Function

Named Arguments

No

Syntax

Choose(index, item1[, item2, ...[,itemn]])

index

Use: Required

Data Type: Single

An expression that evaluates to the number of the item to choose from the list.

item1 - n

Use: Required

Data Type: Variant

A comma-delimited list of values from which to choose.

Return Value

A variant data type item chosen from the list; the data subtype is that of the chosen item.

Description

Programmatically selects an item from a predefined list of values (which are passed as parameters to the function) based on its ordinal position in the list. Using Choose is a simpler alternative to populating an array with fixed values.

Rules at a Glance

  • The list of items is based from 1, rather than the more usual VB default base of 0.

  • Because the list of values is a variant parameter array, you can mix data subtypes within the list; you aren't forced to use the same data subtype for each item in the list. For example, item1 can be a variant string, item2 a long integer, and item3 a floating point number.

  • The list of values can't be expanded or contracted programmatically, but the items within the list can be generated dynamically by including the return value of a function call. For example:

    vChosenOption = Choose(iOption, vFunction1(), _
                    vFunction2(), vFunction3())

Example

Choose is useful for returning some result based on an option button selection. This is most straightforward with Visual Basic, which supports control arrays. For example:

Private Sub Option1_Click(Index ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.