Fill ListView Control with Data from Worksheet

How to fill listview control with data from worksheet, delete data from list view control and transfer data from list-view into text boxes with VBA. Watch the video below:

 

Watch this video on YouTube.

Here’s the complete macro or VBA code to fill a listview control with data from worksheet and how to manipulate the data in the list-view:

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub cmdDelete_Click()
ListView1.ListItems.Remove (ListView1.SelectedItem.Index)
End Sub

Private Sub cmdSearch_Click()
Dim itmx As listItem
Set itmx = ListView1.FindItem(TextBox1.Text, lvwText, , lvwPartial)
If itmx Is Nothing Then
MsgBox “Record not found!”, vbCritical
Else
ListView1.ListItems(itmx.Index).Selected = True
ListView1.SetFocus
End If

Dim myindex As Integer
TextBox1.Text = Me.ListView1.SelectedItem
myindex = Me.ListView1.SelectedItem.Index
TextBox2.Text = Me.ListView1.ListItems.Item(myindex).SubItems(1)
TextBox3.Text = Me.ListView1.ListItems.Item(myindex).SubItems(2)
TextBox4.Text = Me.ListView1.ListItems.Item(myindex).SubItems(3)
End Sub

Private Sub ListView1_BeforeLabelEdit(Cancel As Integer)

End Sub

Private Sub UserForm_Activate()
With Me.ListView1
.HideColumnHeaders = False
.View = lvwReport
End With

‘Now we declare the variables
Dim wksh As Worksheet
Dim rngData As Range
Dim rngCell As Range
Dim listItem As listItem
Dim rowCount As Long
Dim colCount As Long
Dim i As Long
Dim j As Long

Set wksh = Worksheets(“Sheet1”)

Set rngData = wksh.Range(“A1”).CurrentRegion

‘Add column headers
For Each rngCell In rngData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=100
Next rngCell

‘Count number of rows in source range
rowCount = rngData.Rows.Count

‘Count number of columns in source range
colCount = rngData.Columns.Count

‘Now we fill our ListView
For i = 2 To rowCount
Set listItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
For j = 2 To colCount
listItem.ListSubItems.Add Text:=rngData(i, j).Value
Next j
Next i
End Sub

Further reading:

Export ListView to Excel Spreadsheet

ListView

 

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

One thought on “Fill ListView Control with Data from Worksheet”

  1. sir i am getting a error 1004 in exel vba in the given code bellow , can you please help me with this code
    Set myrange = Worksheets(“xyz”).Range(“a:l”)
    Set myrangea = Worksheets(“abc”).Range(“a:m”)
    qnty = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(item, myrange, 6, 0), Application.WorksheetFunction.VLookup(item, myrangea, 4, 0))

Leave a Reply

Your email address will not be published. Required fields are marked *