Fill a ListView on a UserForm

Ferreira1456

New Member
Joined
Mar 20, 2018
Messages
30
Office Version
  1. 365
Platform
  1. 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.


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
Could you please show me what I am doing wrong? Any suggestions you have, are very welcome.

Thank you very much.

Carlos Ferreira
 
Last edited by a moderator:
Hi Dante
I still looking for answering.
I will explain what I need: my ListView shows all my clients. I need the following:
when I input data in txtsearch and press the search button, I want to see all
customers that match the first letter of the names.

In the code below when entering the name exactly as it is in the worksheet
we have the expected result, as I said, I want to see all
customers that match the first letter of the names.

Private Sub cmdPesquisa_Click()


Dim C As Range
Dim li As listItem
Dim sSearch As String
ListView1.ListItems.Clear

sSearch = txtPesquisa

For Each C In Range("NomeDefinidoCliente") 'NomeDefinidoCliente=Define Name


If C.Text = sSearch Or C.Offset(, 16).Text = sSearch Then


Set li = ListView1.ListItems.Add(Text:=C.Offset(, -2).Value) ' ID-

li.ListSubItems.Add Text:=C.Offset(, -1).Value ' Account
li.ListSubItems.Add Text:=C.Offset(, 0).Value ' Name
li.ListSubItems.Add Text:=C.Offset(, 1).Value ' Address
li.ListSubItems.Add Text:=C.Offset(, 2).Value ' City
li.ListSubItems.Add Text:=C.Offset(, 3).Value ' Contacto
li.ListSubItems.Add Text:=C.Offset(, 4).Value ' Phone
li.ListSubItems.Add Text:=C.Offset(, 5).Value ' E-mail
End If
Next C

End Sub

Thank you very much.
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Forget the code for a moment. Explain with your words what you need. What data do you have, with real examples and what are you waiting for? all explained with examples. please, do not put code.
 
Upvote 0
I created a spreadsheet with service data such as: Code, Customer Number, Customer Name, Amount, Quantity, Total, Job Date, Payment Date, etc.

There are 73 columns, using VBA code. I want to create a form that allows searching based on the Customer Name. (Column C3)

When the form is opened the user can type a letter in a text box and all Customers whose names begin with the letter typed will be shown in ListVew with the data in the worksheet.

Double clicking the name of the customer of my choice will automatically fill in the form fields.
 
Upvote 0
For this part:

When the form is opened the user can type a letter in a text box and all Customers whose names begin with the letter typed will be shown in ListVew with the data in the worksheet.


Try this, Capture a letter, you can continue to capture letters and the names are filtered

Code:
[COLOR=#0000ff]Private Sub txtPesquisa_Change[/COLOR]()
    cmdPesquisa_Click
End Sub


[COLOR=#0000ff]Private Sub cmdPesquisa_Click[/COLOR]()
    Dim C As Range, li As ListItem
    
    ListView1.ListItems.Clear
    For Each C In Range("NomeDefinidoCliente") 'NomeDefinidoCliente=Define Name
        If C.Text Like txtPesquisa & "*" Then
            Set li = ListView1.ListItems.Add(Text:=C.Offset(, -2).Value) ' ID-
            li.ListSubItems.Add Text:=C.Offset(, -1).Value ' Account
            li.ListSubItems.Add Text:=C.Offset(, 0).Value ' Name
            li.ListSubItems.Add Text:=C.Offset(, 1).Value ' Address
            li.ListSubItems.Add Text:=C.Offset(, 2).Value ' City
            li.ListSubItems.Add Text:=C.Offset(, 3).Value ' Contacto
            li.ListSubItems.Add Text:=C.Offset(, 4).Value ' Phone
            li.ListSubItems.Add Text:=C.Offset(, 5).Value ' E-mail
        End If
    Next C
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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