vbauserformlistboxitem

How can I reference a specific column of a selected object in a VBA listbox?


I have a listbox compiling information from a worksheet onto a userform. I am trying to figure out how to use the value of a column from the selected object on the listbox. For instance, this is my listbox: enter image description here

I want to get the value in the phone column from a selected row to plug into an xlookup code I already have written: SalesForm.BHSDTAPNAMELF.Value = Application.XLookup(Val(*value from selected row*), Worksheets("MASTER").Range("S:S"), Worksheets("MASTER").Range("T:T"))


Solution

  • Assuming that the MultiSelect property of the listbox is set to fmMultiSelectSingle, you can use the Column property of the listbox to return the value from the specified column of the selected row.

    Therefore, since the phone column is the 7th column, and since the column indexing starts at 0, you can retrieve the desired value as follows...

    Dim selectedItem As String
    selectedItem = UserForm1.ListBox1.Column(6)
    

    Actually, you can use the Me keyword to refer to the userform...

    Dim selectedItem As String
    selectedItem = Me.ListBox1.Column(6)
    

    Change the name of the userform and listbox accordingly.