vba 如何在多列列表框中获取选定的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39244444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get selected value in multicolumn listbox
提问by Yiao SUN
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 Associerand I could get the information of this row. I can just get the first column which is CAN20168301436I want to get the information from the whole line.
How can I do it?
Here is my button clicked event:
就像在照片中一样,我想选择一行,然后单击按钮Associer,我可以获得这一行的信息。我只能得到第一列,这是CAN20168301436我想从整行中获取信息。
我该怎么做?
这是我的按钮单击事件:
Private Sub CommandButton3_Click()
a = ListBoxResultatFind.Text
End Sub
回答by user3598756
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
回答by Olle Sj?gren
No need to loop the entire list - in order to get the selected item row you can use the ListIndexproperty. Then you can use the List(Row, Column)property to retreive the data, as in the examples by @DragonSamu and @user3598756:
无需循环整个列表 - 为了获得所选项目行,您可以使用该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
回答by DragonSamu
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:
或者您可以将所有数据添加到 1 个字符串中:
Dim strColumns as String
strColumns = ListBox1.List(0, 1) + " " + ListBox1.List(0, 2) + " " + ListBox1.List(0, 3)
回答by Mark Vincent Manansala
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.
这是一个 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

