TOP

VBA function: Choose

Description

The VBA Choose function returns a value from its argument list with a given number.


Choose syntax

Choose(number, value_1, value_2, value_3, etc.)

VBA Choose example

Display of one of 3 values by the number written in the "choice" variable:

Sub ChooseExample1()
    
      choice = 1
    
      course = Choose(choice, "Excel", "VBA", "Google Sheets")
    
      MsgBox "Selected course: " & course 'Returns the message: "Selected course: Excel"
    
End Sub

An alternative to the Choose function is to use an array (Array):

Sub ChooseExample2()
    
      choice = 1
    
      course = Array("Excel", "VBA", "Google Sheets"))(choice)
    
      MsgBox "Selected course: " & course 'Returns the message: "Selected course: VBA"
    
End Sub
The first value of the Choose function is at position 1, unlike the array, whose first value is at 0.