Consulting

Results 1 to 4 of 4

Thread: Solved: populate multicolumns of listview

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Solved: populate multicolumns of listview

    hello

    what is the most easy way to populate multiple columns in Listview from some ranges?
    listviewcol. 1 listviewcol. 2 listviewcol. 3
    range 1 range 2 range 3

    and this is my code:

    [VBA]
    Private Sub UserForm_Initialize()
    Dim i As Long
    Dim LR As Long, LC As Long
    Dim cell As Range

    With Sheets("Sheet1")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    With ListView1
    For Each cell In Range("A1", Cells(1, LC))
    .ListItems.Add , , cell
    ListView1_ItemClick .ListItems(.SelectedItem.Index) 'fill edit controls
    Next cell
    For Each cell In Range("A2", Cells(2, LC))

    .ListSubItems.Add , , cell << error
    ListView1_ItemClick .ListItems(.SelectedItem.Index) 'fill edit controls
    Next cell

    .ColumnHeaders.Add , , "Name", Me.TextBox1.Width 'Add columns
    .ColumnHeaders.Add , , "Density", Me.TextBox2.Width
    .HideColumnHeaders = False 'set some properties
    .View = lvwReport
    .Gridlines = True

    End With
    End Sub
    [/VBA]

    thanx for the advice

  2. #2
    VBAX Regular fixo's Avatar
    Joined
    Jul 2006
    Location
    Sankt-Petersburg
    Posts
    99
    Location
    Try this one, it's working good on my end (Excel2007)

        With Sheets("Sheet1")
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            LC = .Cells(1, .Columns.Count).End(xlToLeft).Column'<-- not used in this context
         i = 1
        With ListView1
            For i = 1 To LR
            Set cell = Cells(i, 1)
                .ListItems.Add , , cell
                .ListItems.Item(i).ListSubItems.Add , , cell.Offset(0, 1).Text
            Next i
     
            .ColumnHeaders.Add , , "Name", Me.TextBox1.Width 'Add columns
            .ColumnHeaders.Add , , "Density", Me.TextBox2.Width
            .HideColumnHeaders = False 'set some properties
            .View = lvwReport
            .Gridlines = True
            .FullRowSelect = True
        End With
        End With
    ~'J'~

  3. #3
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    works good on Excel 2003 also. Many thanx

    Antwan

  4. #4
    VBAX Regular fixo's Avatar
    Joined
    Jul 2006
    Location
    Sankt-Petersburg
    Posts
    99
    Location
    Quote Originally Posted by white_flag
    works good on Excel 2003 also. Many thanx

    Antwan
    Glad to help

    Cheers

    ~'J'~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •