How to get selected value in multicolumn listbox

杀马特。学长 韩版系。学妹 提交于 2019-12-01 04:20:35

问题


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:


Just like in the photo, I want to select one line then I will click button Associer and I could get the information of this row. I can just get the first column which is CAN20168301436 I want to get the information from the whole line.
How can I do it?
Here is my button clicked event:

Private Sub CommandButton3_Click()
   a = ListBoxResultatFind.Text
End Sub

回答1:


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



回答2:


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



回答3:


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)



回答4:


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


来源:https://stackoverflow.com/questions/39244444/how-to-get-selected-value-in-multicolumn-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!