Home » excel » Extract data from column of selected row in excel vba userform combobox

Extract data from column of selected row in excel vba userform combobox

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a combobox (cbInventory) populated with a named range in the RowSource property that is a table that contains 6 fields and many thousands of rows. The bound column is the first column (Material). When an item is selected in the combobox, I want the batch number associated with the selected item to automatically appear in a separate text box (using a Change event for cbInventory).

enter image description here

So for example, if A100-114P is selected, the number 11 should appear in the text box.

Private Sub cbInventory_Change()
    Me.txtBatch = Me.cbInventory.Column(1, ?)
End Sub

If I manually enter 1 in place of the question mark above, the number 10 appears in the text box, which makes sense. Now, what do I put in place of the question mark to pull the batch number of the selected item?

How to&Answers:

column(0) would be the 1st column.

Private Sub CommandButton1_Click()
    MsgBox Me.ComboBox1.Column(0)
    MsgBox Me.ComboBox1.Column(1)
    MsgBox Me.ComboBox1.Column(2)

End Sub