Searching Items in List View

Mohsin110

New Member
Joined
Aug 26, 2023
Messages
18
Office Version
  1. 2021
Platform
  1. Windows
Hi, just want to display the items in list view which matching with the textbox text, but my codes are not working for me, could you please help me to correct my code.

VBA Code:
Option Explicit
    Dim wksSource As Worksheet
    Dim rngData As Range
    Dim rngCell As Range
    Dim LstItem As ListItem
    Dim RowCount As Long
    Dim ColCount As Long
    Dim i As Long
    Dim j As Long

'listViw Initialize
Private Sub UserForm_Initialize()
    With Me.ListView1
        .Gridlines = True
        .View = lvwReport
    End With
          pData      
End Sub

'Loading Data to listView
 Sub pData()
   
   
    Set wksSource = ThisWorkbook.Worksheets("mrnData")
    Set rngData = wksSource.Range("A1").CurrentRegion
   
    'column headers
    For Each rngCell In rngData.Rows(1).Cells
        Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=90
    Next rngCell
   
    'Count the number of rows in the source range
    RowCount = rngData.Rows.Count
   
    'Count the number of columns in the source range
    ColCount = rngData.Columns.Count
   
    'Fill the ListView
    For i = 2 To RowCount
        Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
        For j = 2 To ColCount
            LstItem.ListSubItems.Add Text:=rngData(i, j).Value
        Next j
    Next i
   
End Sub

'Searching through listView
Private Sub TextBox1_Change() 
 Dim fString As Variant
    fString = TextBox1.Text & "*"
       
        pData
        With Me.ListView1
                For i = .ListItems.Count To 1 Step -1
                        If Not (LCase(.ListItems(i) Like LCase(fString))) Then
                            .ListItems.Remove i
                    End If
                Next i
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I haven't look at your sample workbook, but I would suggest that you take another approach. When the Change event is executed, first clear the ListView, then loop through your data, and then load onto the ListView any row that meets the criteria.

VBA Code:
Private Sub TextBox1_Change()
    Dim fString As Variant
    fString = TextBox1.Text & "*"
    
    With Me.ListView1
        .ListItems.Clear
        For i = 2 To RowCount
            If LCase(rngData(i, 1)) Like LCase(fString) Then
                Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
                For j = 2 To ColCount
                    LstItem.ListSubItems.Add Text:=rngData(i, j).Value
                Next j
            End If
        Next i
    End With
End Sub

Hope this helps!
 
Upvote 1
The Item property of the ListSubItems object requires that you specify an index number that points to a subitem. For example, if you want to change the forecolor for the first subitem for a listitem...

VBA Code:
LstItem.ListSubItems.Item(1).ForeColor = RGB(173, 189, 190)

And you'll need to individually set the forecolor for each subitem for the listitem. And you'll need to set the forecolor for the listitem itself, if what you want is to set the forecolor for the entire row. So, for example, if you want to set the forecolor for the entire row...

VBA Code:
    Dim i As Long
    Dim j As Long
    Dim RowColor As Long
    Dim LstItem As ListItem
    Dim LstSubItem As ListSubItem
    
    'Fill the ListView
    For i = 2 To RowCount
    
       'Alternate row color
        If i Mod 2 = 0 Then
            RowColor = RGB(173, 189, 190)
        Else
            RowColor = RGB(18, 189, 180)
        End If
        
        Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
        
        LstItem.ForeColor = RowColor
        
        For j = 2 To ColCount
            Set LstSubItem = LstItem.ListSubItems.Add(Text:=rngData(i, j).Value)
            LstSubItem.ForeColor = RowColor
        Next j
        
    Next i

Hope this helps!
 
Upvote 1
Solution
As you probably have already noticed, the ListView object does not contain a BackColor property. So, unfortunately, it looks like it's not possible.

Cheers!
 
Upvote 1
Please explain what you mean by “is not working”.

Also provide a sample worksheet with data you are displaying in the Listview control.
 
Upvote 0
The data is loading successfully in list view from excel table but searching by search box in list view is not working, the problem is only with searching part of code i think. i have attached the excel workbook please check & help.

Sample = MyWorkbook
 
Upvote 0
I haven't look at your sample workbook, but I would suggest that you take another approach. When the Change event is executed, first clear the ListView, then loop through your data, and then load onto the ListView any row that meets the criteria.

VBA Code:
Private Sub TextBox1_Change()
    Dim fString As Variant
    fString = TextBox1.Text & "*"
   
    With Me.ListView1
        .ListItems.Clear
        For i = 2 To RowCount
            If LCase(rngData(i, 1)) Like LCase(fString) Then
                Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
                For j = 2 To ColCount
                    LstItem.ListSubItems.Add Text:=rngData(i, j).Value
                Next j
            End If
        Next i
    End With
End Sub

Hope this helps!
I am really grateful, you have solve my problem, thank you so much.
I have to add reference of all the columns in your suggested code to search through all the columns in list view. Its working perfectly now.

VBA Code:
Private Sub TextBox1_Change()
   Dim fString As Variant
   Dim s As Long
   
    fString = TextBox1.text & "*"
    
    With Me.ListView1
        .ListItems.Clear
        For s = 2 To RowCount
            If (LCase(rngData(s, 1)) Like LCase(fString) Or LCase(rngData(s, 2)) Like LCase(fString) _
                Or LCase(rngData(s, 4)) Like LCase(fString) Or LCase(rngData(s, 5)) Like LCase(fString) _
                Or LCase(rngData(s, 6)) Like LCase(fString) Or LCase(rngData(s, 7)) Like LCase(fString) _
                Or LCase(rngData(s, 7)) Like LCase(fString) Or LCase(rngData(s, 8)) Like LCase(fString) _
                Or LCase(rngData(s, 9)) Like LCase(fString) Or LCase(rngData(s, 10)) Like LCase(fString) _
                Or LCase(rngData(s, 11)) Like LCase(fString) Or LCase(rngData(s, 16)) Like LCase(fString)) Then
            
                Set LstItem = .ListItems.Add(text:=rngData(s, 1).Value)
                For j = 2 To ColCount
                    LstItem.ListSubItems.Add text:=rngData(s, j).Value
                Next j
            End If
        Next s
    End With
End Sub
 
Upvote 0
Hi, I fill the values in form by double click row of list view, to edit some values and then press the Update button to save changes, but it just able to make changes only in the last row, rest of rows do not update by pressing update button, hope you understand what I'm saying. The update button VBA code is mentioned below please help me if there is any problem in the code. Is there any option to send you a video to review?

VBA Code:
Sub UpdateBtn()

    Set sh = ThisWorkbook.Sheets("MRN")                 'Sheet 1
    Set sh2 = ThisWorkbook.Sheets("mrnData")          'Sheet 2
    Set tbl = sh2.ListObjects("mrnDbase")                   'Table
   
    LR = sh2.Cells(Rows.Count, 1).End(xlUp).Row        'Table rows
        
    For l = 2 To LR
        If sh2.Cells(LR, 1).Value = Range("ID").Value Then
            With sh2
                '.Cells(LR, 1) = ID
                '.Cells(LR, 2) = Range("ref").Value
                '.Cells(LR, 3) = Range("Date").Value
                .Cells(LR, 4) = Range("Supplier").Value
                .Cells(LR, 5) = Range("PO").Value
                .Cells(LR, 6) = Range("Mode").Value
                .Cells(LR, 7) = Range("Forwarder").Value
                .Cells(LR, 8) = Range("CustomDeclaration").Value
                .Cells(LR, 9) = Range("AWB").Value
                .Cells(LR, 10) = Range("EDAS").Value
                .Cells(LR, 11) = Range("CustomDuty").Value
                .Cells(LR, 12) = Range("DisbursFee").Value
                .Cells(LR, 13) = Range("OtherBOE").Value
                .Cells(LR, 14) = Range("VAT").Value
                .Cells(LR, 15) = Range("AdminFee").Value
                .Cells(LR, 16) = Range("Fquote").Value
                .Cells(LR, 17) = Range("ExpressFre").Value
                .Cells(LR, 18) = Range("DocAttest").Value
                .Cells(LR, 19) = Range("Handling").Value
                .Cells(LR, 20) = Range("ACombinedPO").Value
                .Cells(LR, 21) = Range("Combined").Value
                .Cells(LR, 22) = Range("Note").Value
            End With
        End If
    Next l
End Sub
 

Attachments

  • Form.png
    Form.png
    47.7 KB · Views: 7
  • mrnDATA sheet.png
    mrnDATA sheet.png
    23.7 KB · Views: 7
Upvote 0
The logic in the IF statement inside your loop appears to be the problem ....
Your code is
VBA Code:
If sh2.Cells(LR, 1).Value = Range("ID").Value Then

This only updates row LR.

Try changing your code to
VBA Code:
        If sh2.Cells(l, 1).Value = Range("ID").Value Then
(change "LR" to "l") and see if that works better for you.
 
Upvote 0

Forum statistics

Threads
1,215,727
Messages
6,126,521
Members
449,316
Latest member
sravya

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