Reputation: 61
I used OnKeyUp so while user is typing, it is searching in the list view. I am getting an error with my code.
"Wrong number of arguments or invalid property assignment"
Here is my code below:
Private Sub txtEmailGenSearch_KeyUp(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
Dim strText As String
Dim i As Long
strText = LCase(txtSearch.value)
With MainForm.lstMailGen
For i = 0 To .ListItems.count - 1
If LCase(Left(.ListItems(i, 0), Len(strText))) = strText Then
Exit For
Next i
If i = .ListItems.count Then
' No matching item was found, select nothing
.ListIndex = -1
Else
' A match was found, select it
.ListIndex = i
End If
End With
End Sub
Upvotes: 0
Views: 3097
Reputation: 7759
A ListView is quite different from a ListBox. Each row in a ListView is a ListItem. If the ListView has multiple columns then each ListItem will contain ListSubItems. This applies to a Microsoft Windows Common Controls 6.0 Listview, 5.0 works a little different. ListViews do not return a 2D array like a ListBox does have have a ListIndex property.
Recommended Reading: Excel VBA ListView Control Examples
Use ListItem.Find()
to locate a matching ListItem
With MainForm.lstMailGen Dim item As ListItem Set item = .FindItem(sz:=txtSearch.value, fPartial:=lvwPartial) If Not item Is Nothing Then item.Selected = True End If End With
In for the ListItem to be hightlighted make sure that HideSelection = False
MainForm.lstMailGen.HideSelection = False
The Listitems first index is 1 not 0.
For i = 1 To .ListItems.Count If LCase(Left(.ListItems(i), Len(strText))) = strText Then Exit For Next i
If the last item contained the string than If i = .ListItems.count Then
would skip the selection. If i > .ListItems.count Then
is the right way to do this. If the For
loop completes then i
will be incremented an extra time. In the above case i
would = .ListItems.Count + 1` if the loop completed.
Upvotes: 1