Ferreira1456
New Member
- Joined
- Mar 20, 2018
- Messages
- 30
- Office Version
- 365
- Platform
- Windows
The following code fills a ListView on a UserForm with data from an Excel worksheet.
I am trying to search in the ListView.
I would like to search the textbox that is in the 3rd column sheet1 (column C)."txtNomeCliente"
Below, is the code that I am trying to adapt for my specific needs.
Could you please show me what I am doing wrong? Any suggestions you have, are very welcome.
Thank you very much.
Carlos Ferreira
I am trying to search in the ListView.
I would like to search the textbox that is in the 3rd column sheet1 (column C)."txtNomeCliente"
Below, is the code that I am trying to adapt for my specific needs.
Code:
Private Sub UserForm_Initialize()
With Me.ListView1 .Gridlines = True
.View = lvwReport
.FullRowSelect = True
.HideColumnHeaders = False
End With
Call LoadListView
End Sub
'-----------------------------------------------------------------------
Private Sub LoadListView()
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
Set wksSource = Worksheets("Cliente")
Set rngData = wksSource.Range("A2").CurrentRegion
For Each rngCell In rngData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=10
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 = 3 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
'-----------------------------------------------------------------------
Private Sub cmdSearch_Click()
Dim itmx As listItem
Dim myindex As Integer
Set itmx = ListView1.FindItem(txtNomeCliente.Text, lvwText, , lvwPartial)
If itmx Is Nothing Then
MsgBox "Not Record", vbCritical
Else
ListView1.ListItems(itmx.Index).Selected = True
End If
txtClienteID.Text = Me.ListView1.SelectedItem
myindex = Me.ListView1.SelectedItem.Index
txtContaNumero.Text = Me.ListView1.ListItems.Item(myindex).SubItems(1)
txtClienteID.Text = Me.ListView1.ListItems.Item(myindex).SubItems(2)
txtEnderecoCliente.Text = Me.ListView1.ListItems.Item(myindex).SubItems(3)
End Sub
Thank you very much.
Carlos Ferreira
Last edited by a moderator: