Listview Find item

tonyml

New Member
Joined
Jun 3, 2022
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone, I need help with the search in a listview, I have a listview with a textbox with a search button.
Sorry because I´m new using VBA and I´m struggle my mind at don´t know how to do.

At the moment I am using this code it works fine for me, but it only indicates the first match, so I have two questions:
1. How can I make it continue indicating the next match?
2. How I implement the use of wildcards(*) in search?

Thank you very much in advance

code:

Private Sub CommandButton1_Click()

Dim itmx As ListItem
Set itmx = ListView1.FindItem(TextBox1.Text, lvwSubItem, , lvwPartial)

If itmx Is Nothing Then
MsgBox "No matches found", vbExclamation, "Information"
TextBox1.Value = Clean
TextBox1.SetFocus
Else
ListView1.ListItems(itmx.Index).Selected = True
ListView1.SetFocus
End If

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The lvwPartial setting tries to match an item whose Text property begins with the string being searched. So you'll need to loop through the subitems yourself. For example, the following code will loop through the subitems for each listitem, and find the subitem whose text contains the search text. And, for each match, it prints to the Immediate Window the text for the listitem, along with the text for the subitem. Note that the comparison is not case-sensitive.

VBA Code:
Private Sub CommandButton1_Click()

    Dim searchText As String
    searchText = Me.TextBox1.Value
   
    If Len(searchText) = 0 Then
        MsgBox "Enter a search term, and try again!", vbExclamation
        Exit Sub
    End If
   
    Dim itm As ListSubItem
    Dim i As Long
    Dim j As Long
    With Me.ListView1
        For i = 1 To .ListItems.Count
            With .ListItems(i)
                For j = 1 To .ListSubItems.Count
                    If LCase(.ListSubItems(j)) Like "*" & LCase(searchText) & "*" Then
                        Debug.Print .Text, .ListSubItems(j)
                        Exit For
                    End If
                Next j
            End With
        Next i
    End With
   
   
End Sub

Hope this helps!
 
Upvote 0
Domenic, note that your code doesn't find the search string if it occurs in the first column. For the first column add this immediately after the With .ListItems(i):
VBA Code:
                If LCase(.Text) Like "*" & LCase(searchText) & "*" Then
                    Debug.Print .Text
                    Exit For
                End If
 
Upvote 0
Hi John, thanks for pointing that out.

I assumed that the he's looking for a partial match in the subitems, since lvwSubItem was being used as theWhere argument. But after re-reading the original post, you may very well be right.

So I've amended the macro as per your suggestion. Although, since he's looking for multiple matches, I tweaked it a bit.

Thanks again, cheers!

VBA Code:
Private Sub CommandButton1_Click()

    Dim searchText As String
    searchText = Me.TextBox1.Value
   
    If Len(searchText) = 0 Then
        MsgBox "Enter a search term, and try again!", vbExclamation
        Exit Sub
    End If
   
    Dim itm As ListSubItem
    Dim i As Long
    Dim j As Long
    With Me.ListView1
        For i = 1 To .ListItems.Count
            With .ListItems(i)
                If LCase(.Text) Like "*" & LCase(searchText) & "*" Then
                    Debug.Print .Text
                Else
                    For j = 1 To .ListSubItems.Count
                        If LCase(.ListSubItems(j)) Like "*" & LCase(searchText) & "*" Then
                            Debug.Print .Text, .ListSubItems(j)
                            Exit For
                        End If
                    Next j
                End If
            End With
        Next i
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,165
Messages
6,123,390
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top