如何使用 vba 代码 Select 从多选列表框中输入的值

How to Select the entered values from the Multiselect listbox using vba code

我有一个文本框和一个具有以下值的多选列表框

当我在文本框中输入值并单击搜索时,它应该 select 列表框中的特定值。我正在使用以下代码

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

我想得到下面的结果

但它 select 是索引而不是值。例如

如何select值而不是索引?

你的这个代码 selects 按项目,而不是值:

For Each itm In strArray 
    lstAnswer.Selected(itm) = True 
Next

按值 select,对于每个值循环列表值,如果找到,将找到的项目标记为 selected。

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