我有一个文本框和一个MultiSelect列表框,其值如下

当我在文本框中输入值并单击search时,它应该选择列表框中的特定值。我正在使用下面的代码
Private Sub Search_Click()
Dim str As String
Dim c As Collection
Dim strArray() As String
Dim intcnt As Integer
str = txtAnswer.Value
strArray = Split(str, ",")
For Each itm In strArray
lstAnswer.Selected (itm)= True
Next
End Sub我想得到下面的结果

但是它选择索引而不是值。例如

如何选择值而不是索引?
发布于 2015-11-23 07:14:38
Private Sub Search_Click()
Dim ItM As String
Dim c As Collection
Dim strArray() As String
Dim intcnt As Integer
strArray = Split(txtAnswer.Value, ",")
With lstAnswer
For Each ItM In strArray
For i = 0 To .ListCount - 1
If .List(i) <> ItM Then
Else
.Selected(i) = True
Exit For
End If
Next i
Next ItM
End With
End Sub发布于 2015-11-23 07:10:24
您的代码按项(而不是值)进行选择:
For Each itm In strArray
lstAnswer.Selected(itm) = True
Next若要按值进行选择,请为每个值循环列表值,如果找到,则将已找到的项标记为选定项。
https://stackoverflow.com/questions/33864309
复制相似问题