0

I have a Textbox and a MultiSelect listbox with following values

enter image description here

When i enter the values in the text box and click on search it should select the particular values in the listbox. I'm Using the below code

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

I want to get the below result

enter image description here

But It selects the index instead of values. for Example

enter image description here

How to select the values instead of index?

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

1

This code of yours selects by item, not value:

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

To select by value, for each value loop the list values and, if found, mark the found item as selected.

2 Comments

Thank you for the response it would be helpful if you share the sample code
True, but I figured you could found out yourself having the method. But @R3uK served if for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.