How to get selected value in multicolumn listbox

后端 未结 4 1696
长发绾君心
长发绾君心 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条回答
  • 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
    
    0 讨论(0)
  • 2020-12-06 22:10

    It's a 6column list box and the 3rd column would be the multiplier hence the "(x)". You may also rearrange the list to how you like it.

    Private Function selList() As String
    Dim i As Long
    
    For i =LBound(lstListBox1.List) To UBound(lstListBox1.List)
        If lstListBox1.Selected(i) Then
            selList = selList & lstListBox1.List(i) & " " & lstListBox1.List(i, 1) _
            & "(x" & lstListBox1.List(i, 3) & ")" & " " & lstListBox1.List(i, 2) & " " & lstListBox1.List(i, 4) & ", "
        End If
    Next i
    
    If selList= "" Then
        selList= ""
    Else
        selList= Left(selList, Len(selList) - 2)
    End If
    
    MsgBox selList
    End Function
    
    0 讨论(0)
  • 2020-12-06 22:13

    No need to loop the entire list - in order to get the selected item row you can use the ListIndex property. Then you can use the List(Row, Column) property to retreive the data, as in the examples by @DragonSamu and @user3598756:

    '***** Verify that a row is selected first
    If ListBoxResultatFind.ListIndex > -1 And ListBoxResultatFind.Selected(ListBoxResultatFind.ListIndex) Then
        '***** Use the data - in my example only columns 2 & 3 are used
        MsgBox ListBoxResultatFind.List(ListBoxResultatFind.ListIndex, 1) & ":" & ListBoxResultatFind.List(ListBoxResultatFind.ListIndex, 2)
    End If
    
    0 讨论(0)
  • 2020-12-06 22:20

    With a single column you can retrieve the value as below:

    Dim str as String
    str = me.ListBox1.Value
    

    With a multicolumn you can retrieve the value like this:

    Dim strCol1 as String
    Dim strCol2 as String
    Dim strCol3 as String
    strCol1 = ListBox1.List(0, 1)
    strCol2 = ListBox1.List(0, 2)
    strCol3 = ListBox1.List(0, 3)
    

    or you can add all the data into 1 String:

    Dim strColumns as String
    strColumns = ListBox1.List(0, 1) + " " + ListBox1.List(0, 2) + " " + ListBox1.List(0, 3)
    
    0 讨论(0)
提交回复
热议问题