You'll want to loop over the items in the list until you find a match, once you find the match make it selected using the index of the loop.
Private Sub CommandButton1_Click()
Dim i As Integer For i = 0 To Me.ListBox1.ListCount - 1 If Me.ListBox1.List(i) = "Hi4" Then Me.ListBox1.ListIndex = i End If Next
End Sub
Private Sub UserForm_Activate() Me.ListBox1.AddItem "Hi1" Me.ListBox1.AddItem "Hi2" Me.ListBox1.AddItem "Hi3" Me.ListBox1.AddItem "Hi4" Me.ListBox1.AddItem "Hi5" End Sub
The code above is for a user form with a list box and command button. The list box is populated with the values when the form activates, pressing the command button will look for "Hi4", and highlights it when found.
Not to worry the same logic still applies. You'll still want to loop over the listview's items and again you'll want to set the selected index to the loops value if an item is found. The only difference is you have to work with columns as well. Don't seem to have the list view control in Excel here so there is no code.
about listview in vba excel
DalenCa
Hello again,
You'll want to loop over the items in the list until you find a match, once you find the match make it selected using the index of the loop.
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.List(i) = "Hi4" Then
Me.ListBox1.ListIndex = i
End If
Next
End Sub
Private Sub UserForm_Activate()
Me.ListBox1.AddItem "Hi1"
Me.ListBox1.AddItem "Hi2"
Me.ListBox1.AddItem "Hi3"
Me.ListBox1.AddItem "Hi4"
Me.ListBox1.AddItem "Hi5"
End Sub
The code above is for a user form with a list box and command button. The list box is populated with the values when the form activates, pressing the command button will look for "Hi4", and highlights it when found.
JustStarted
Derek the commands work well with the listbox but i am using listview.
I hope you will still answer me..
Thnx again
FSV...
Kishor_Tripathy
Hey FSV,
Not to worry the same logic still applies. You'll still want to loop over the listview's items and again you'll want to set the selected index to the loops value if an item is found. The only difference is you have to work with columns as well. Don't seem to have the list view control in Excel here so there is no code.