Yia*_*SUN 4 excel vba listbox excel-vba userform
我的userform中有一个多列列表框,我希望得到列表框中所选行中所有元素的值.
这是我的用户形式:
就像在照片中一样,我想选择一行然后我会点击按钮Associer,我可以获得这一行的信息.我可以得到第一列,CAN20168301436我想从整行获取信息.
我该怎么做?
这是我点击的按钮事件:
Private Sub CommandButton3_Click()
a = ListBoxResultatFind.Text
End Sub
Run Code Online (Sandbox Code Playgroud)
use*_*756 14
你可以使用这段代码
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
Run Code Online (Sandbox Code Playgroud)
Oll*_*ren 11
无需循环整个列表 - 为了获得所选项目行,您可以使用该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
Run Code Online (Sandbox Code Playgroud)
使用单个列,您可以检索如下值:
Dim str as String
str = me.ListBox1.Value
Run Code Online (Sandbox Code Playgroud)
使用多列,您可以像这样检索值:
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)
Run Code Online (Sandbox Code Playgroud)
或者您可以将所有数据添加到 1 个字符串中:
Dim strColumns as String
strColumns = ListBox1.List(0, 1) + " " + ListBox1.List(0, 2) + " " + ListBox1.List(0, 3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36262 次 |
| 最近记录: |