unpix
unpix

Reputation: 853

Jump to an item in Listbox

One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?

enter image description here

I already tried the following code

Listbox.ListIndex = index

But it drives me to the error You've used the ListIndex property incorrectly

One property of my list that might be important to mention.

Row source type : Table/Query

Thank you in advance.

Upvotes: 0

Views: 1327

Answers (2)

user2140173
user2140173

Reputation:

Create a standard module with the code

Sub Main()
    UserForm1.Show
    Unload UserForm1
End Sub

Insert a userform and visually do something like

enter image description here

Go into the userform code and add

Private Sub CommandButton1_Click()

    Dim v As Long
    For v = 0 To ListBox1.ListCount - 1
        If TextBox1 = ListBox1.List(v) Then
            ListBox1.Selected(v) = True
        End If
    Next v

End Sub

Private Sub UserForm_Initialize()

    With ListBox1
        .AddItem ("text1")
        .AddItem ("text2")
        .AddItem ("text3")
    End With

End Sub

Run Main Macro

Type in the box : text2

The text2 will be selected in the list

enter image description here

Upvotes: 1

Peter Albert
Peter Albert

Reputation: 17475

Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.

Upvotes: 2

Related Questions