我有一个显示通讯组成员的列表框。我有用于多重简单选择的列表框设置,以选择多个成员。我正在尝试执行循环来获取所选项目(索引)的文本。这是我的代码,对于第一项效果很好。在第二项上,它崩溃并显示“索引超出了数组的范围”。我将把这些成员放在 session 请求的“收件人:”字段中。所以我需要这样格式化:member1;member2。我只是使用 MsgBox 来测试我得到的结果。但我猜我需要添加到一个数组中。请帮忙!
For i = 0 To (myListBox.Items.Count - 1)
If myListBox.GetSelected(i) Then
MsgBox(myListBox.SelectedItems(i))
End If
Next
最佳答案
重新阅读问题,我认为您想将其存储为字符串。这应该可以满足您的要求:
Documentation on ListBox.Selected()
Dim MailStr as String
MailStr = ""
If myListBox.SelectedItems.Count = 0 Then
MsgBox "No User Selected"
Exit Sub
End If
For i = 0 to (myListBox.Items.Count - 1)
If myListBox.Selected(i) Then
MailStr = MailStr & myListBox.Items.Item(i) & "; "
End If
Next i
您还可以尝试:
Dim assets As String
assets = ""
For Each item In assetListBox.SelectedItems
If assets = Nothing Then
assets = item
'Outputs 1 item "Member1"
MsgBox(assets)
Else
assets = assets & "; " & item
'Outputs 2 items in format "member1; member2"
MsgBox(assets)
End If
Next
关于vba - 获取listBox.selectedItems的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28176259/