Filling Columns in ListView

Jaybe08

New Member
Joined
Jul 9, 2010
Messages
8
I have an Excel sheets that contains three columns.
Column A = First Name
Column B = Last Name
Column C = State

I created a ListView and have created the 3 column headers.
However, I don't know how to populate all the columns. Do I need to assign a key to each column? If so, how? I appreciate any help.

I know how to populate the first column. Here's my code for that.

Dim cell As Range
With lvAgreements
For Each cell In Range("A1:A14")
.ListItems.Add , , cell
Next cell
End With

Thanks again for any help.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
When you say you 'created' a ListView you do mean you used the standard ListView control don't you?

Where is it located?

Worksheet? Userform?

Where is the data located?
 
Upvote 0
When you say you 'created' a ListView you do mean you used the standard ListView control don't you?

Where is it located?

Worksheet? Userform?

Where is the data located?

Yes, I used the standard ListView control.
It's located within a Userform.
The data is located in a worksheet.
 
Upvote 0
I guess this is what I need help with.

With lvAgreements
For Each cell In Range("A4:A14") 'Fill column 1
.ListItems.Add , , cell
Next cell

For Each cell In Range("B4:B14") 'Fill column 2
'What would go here?
Next cell
End With
 
Upvote 0
Well I got this to work.
Code:
Option Explicit
 
Private Sub CommandButton4_Click()
    Unload Me
End Sub
 
Private Sub CommandButton5_Click()
    lvAgreements.SortKey = 0
    lvAgreements.Sorted = True
End Sub
 
Private Sub CommandButton6_Click()
    lvAgreements.SortKey = 2
    lvAgreements.Sorted = True
End Sub
 
Private Sub CommandButton7_Click()
    lvAgreements.SortKey = 1
    lvAgreements.Sorted = True
End Sub
 
Private Sub UserForm_Initialize()
Dim cell As Range
Dim lstItem As ListItem
Dim rngHdrs As Range
Dim lstCol As ColumnHeader
Dim rng As Range
 
    Set rngHdrs = Range("A1:C1")
 
    With lvAgreements

        For Each rng In rngHdrs
            Set lstCol = .ColumnHeaders.Add(, , rng.Value)
            lstCol.Width = .Width / 4
        Next rng
 
        .View = lvwReport
 
        For Each cell In Range("A2:A14")
 
            Set lstItem = .ListItems.Add(, , cell.Value)
            lstItem.SubItems(1) = cell.Offset(, 1).Value
            lstItem.SubItems(2) = cell.Offset(, 2).Value
        Next cell
 
    End With
 
End Sub
See this file I uploaded.

It's pretty rough and ready - it's the first time I've really worked with list views in Excel VBA.

I've used them elsewhere, eg Visual Studio, and taken advantage of such handy things as the ListViewItem Collection Editor.:)

PS If the upload doesn't work try the code with a userform with 4 command buttons (3 for sorting, 1 for closing the form) and a listview.

You might find that there might be some options you don't want - went a bit crazy.

HoverSelection? What's that and why can't I turn it off manually.:eek:
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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