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: