How to get selected value in multicolumn listbox

后端 未结 4 1722
长发绾君心
长发绾君心 2020-12-06 21:25

I have a multicolumn listbox in my userform and I would like to get all the values of the elements which are in the selected row in the listbox.

Here is my userform:

4条回答
  •  猫巷女王i
    2020-12-06 22:02

    you can use this code

    Private Sub CommandButton3_Click()
        Dim strng As String
        Dim lCol As Long, lRow As Long
    
        With Me.ListBox1 '<--| refer to your listbox: change "ListBox1" with your actual listbox name
            For lRow = 0 To .ListCount - 1 '<--| loop through listbox rows
                If .selected(lRow) Then '<--| if current row selected
                    For lCol = 0 To .ColumnCount - 1 '<--| loop through listbox columns
                        strng = strng & .List(lRow, lCol) & " | " '<--| build your output string
                    Next lCol
                    MsgBox "you selected" & vbCrLf & Left(strng, (Len(strng) - 1)) '<--| show output string (after removing its last character ("|"))
                    Exit For '<-_| exit loop
                End If
            Next lRow
        End With
    End Sub
    

提交回复
热议问题