Jeffrey Kramer
Jeffrey Kramer

Reputation: 1345

Refer to value selected in combo box with two columns on user form

I have a user form with a combo box with two columns. How do I refer to the values that are active in the combo box within VBA script on other parts of the user form (buttons, etc)?

Simply using MyComboBox.Value yields the value in the first column. I can't figure out how to refer to the second column.

EDIT: Based on advice below, I used this:

MyComboBox.List(MyComboBox.ListIndex, 1)

Upvotes: 0

Views: 3032

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71247

You don't.

The Value of a multi-column ComboBox will be the "key" value. You've presumably populated the dropdown from some list or array: use that same source to lookup the value corresponding to the selected key. Note the key/value wording: having the values in a Dictionary makes it very easy to retrieve the value of the second column.

Or, look it up from the control's List (which is essentially a copy of your items source), using the ListIndex, which gives you the index of the selected item in the source list.

You can have a Property Get procedure responsible for this lookup - then other parts of the code can easily consume it as needed.

Private Property Get SelectedItemDisplayText() As String
    SelectedItemDisplayText = MyComboBox.List(MyComboBox.ListIndex, 1)
End Property

Normally you only care for the selected "key" (/"ID"), not the "display value" though.

Upvotes: 1

Related Questions