Log in

View Full Version : set ListView on userform



Mahesh
12-02-2012, 10:32 PM
PFA workbook.
Please help me to set the ListView on userform as per mention in workbook

Admin
12-03-2012, 08:52 AM
Hi

Replace the userform module code with the following.


Private Sub CommandButton1_Click()
'SEARCH
Dim Cnt As Long
Dim FirstAddx As String
Dim FoundMatch As Range
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Dim Wks As Worksheet
Dim Rng As Range
Dim lstItem As ListItem


StartRow = 2
Set Wks = Sheets(1)

If TextBox1.Text = "" Then
MsgBox "Please enter a search term."
TextBox1.SetFocus
Exit Sub
End If

Set Rng = Wks.Range("A2:g" & Wks.Range("a" & Wks.Rows.Count).End(3).Row)
Set FoundMatch = Rng.Find(What:=TextBox1.Text, LookAt:=xlPart, MatchCase:=False)

If Not FoundMatch Is Nothing Then
FirstAddx = FoundMatch.Address
ListView1.ListItems.Clear
For R = 1 To Rng.Rows.Count
ListView1.ListItems.Add R
Next
Do
Cnt = FoundMatch.Column
R = FoundMatch.Row
If Cnt = 1 Then
ListView1.ListItems(R - 1).Text = FoundMatch.Text
Else
Set lstItem = ListView1.ListItems(R - 1)
lstItem.SubItems(Cnt - 1) = FoundMatch.Text
End If
Set FoundMatch = Rng.FindNext(FoundMatch)
Loop While FoundMatch.Address <> FirstAddx And Not FoundMatch Is Nothing
SearchRecords = Cnt
Else
ListView1.ListItems.Clear
SearchRecords = 0
MsgBox "No match found for " & TextBox1.Text
End If

End Sub

Private Sub UserForm_Activate()

Dim C As Long
Dim I As Long
Dim R As Long
Dim Wks As Worksheet

With ListView1
.Gridlines = True
.View = lvwReport
.HideSelection = False
.FullRowSelect = True
.HotTracking = True
.HoverSelection = False
End With

Set Wks = Sheets(1)

For C = 1 To 7
ListView1.ColumnHeaders.Add Text:=Wks.Cells(1, C).Text
Next C

End Sub

Mahesh
12-04-2012, 12:12 AM
thank u so much Admin for u r help and time..! :cheerclink:

Admin
12-04-2012, 08:50 AM
Thanks for the feedback :cheers: