如何在多列列表框中获取选定的值

我在我的用户表单中有一个多列列表框,我想要获取列表框中所选行中元素的所有值。

这是我的用户表单: UserForm与ListBox

就像在照片中,我想select一行,然后点击buttonAssocier ,我可以得到这一行的信息。 我可以得到CAN20168301436的第一列我想从整个行中获取信息。
我该怎么做?
这是我点击button的事件:

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

你可以使用这个代码

 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 

无需循环整个列表 – 为了获得选定的行项目,您可以使用ListIndex属性。 然后,您可以使用List(Row, Column)属性来检索数据,如@DragonSamu和@ 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 

使用单个列可以检索如下的值:

 Dim str as String str = me.ListBox1.Value 

用多列可以检索如下的值:

 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) 

或者你可以将所有的数据添加到1string:

 Dim strColumns as String strColumns = ListBox1.List(0, 1) + " " + ListBox1.List(0, 2) + " " + ListBox1.List(0, 3) 

这是一个6列的列表框,第3列是乘数因此是“(x)”。 你也可以重新排列你喜欢的列表。

 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