0

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
2
  • What is your question? Where is your issue? What's wrong with your code? You need to ask something if you want us to answer (see How to Ask). Commented Nov 29, 2018 at 12:52
  • In which line do you get the error? You need to be as specific and detailed as possible. • Note that your If LCase … Then statement has no End If Commented Nov 29, 2018 at 13:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi I keep getting an error in the .ListItems(i,0) part. It says Wrong number of arguments or invalid property assignment.
@RobinScherbatsky I made an error editing the code. In any case .ListItems is a collection not a 2D array. Use .ListItems(i). If the value is in the second column you would use `.ListItems(i).ListSubItems(1).

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.