Search code examples
excelvbacomboboxuserform

Displays two values (columns) in the mode of selecting an item from the combobox -VBA


Combobox1 lists two columns of the values of cells A and B in Sheet1 Excel. But by choosing one item, only the cell A is displayed (and cell B hidden). How to display both values in the selection mode?

My VBA Code:

Private Sub UserForm_Initialize()
Dim xrg   As Range
Set xrg = ThisWorkbook.Worksheets("Sheet1").Range("A1:B5")
With Me.ComboBox1
  .List = xrg.Value
  .ColumnCount = 2
End With
End Sub

Solution

  • you simply can't

    but you can work around it:

    • place two text boxes each overlapping one "column" in the combobox value display

    textboxes overlapping combobox

    enter image description here

    enter image description here

    • place the following code in the userform code pane:

      Private Sub ComboBox1_Change()
          If Me.ComboBox1.ListIndex > -1 Then
              Me.TextBox1.Value = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 0)
              Me.TextBox2.Value = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1)
          End If
      End Sub
      

    (change textboxes name to match your choice)