Consulting

Results 1 to 8 of 8

Thread: VBA to Add ListItems to a ListView

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    VBA to Add ListItems to a ListView

    I'm trying to add ListItems from a Dynamic range to a Listview.
    This is the code that I have.
    [VBA]
    Private Sub UserForm_Initialize()

    ' To fill ListView1 with content from a worksheet range

    Sheets("Sheet1").Activate

    With ListView1
    .View = lvwReport
    With .ColumnHeaders
    .Clear
    .Add , , "Account #", 100
    .Add , , " Name", 150
    .Add , , " Total", 80
    End With
    .ListItems.Add , , Sheets(1).Range(Range("A2"), Range("C65536").End(xlUp))

    .HideColumnHeaders = False
    .Appearance = cc3D
    .FullRowSelect = True

    End With
    End Sub
    [/VBA]

    It works when I add just 1 item in Col A, but I want to add all the data in the range from Col A to the last used row in column C .

    Cant seem to find good info on Listview. Any help appreciated.

    Thanks

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]
    for each cl in sheets(1).columns(1).specialcells(2)
    .listitems.add,,cl
    next
    [/VBA]

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    snb: Thanks.

    However, that only populates column 1.

    I did get t his to work like this.
    [VBA]
    With Sheets("Sheet1")
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    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
    .ListItems.Item(i).ListSubItems.Add , , cell.Offset(0, 2).Text
    Next i
    End With [/VBA]

    Thanks for your input.
    BTW: How do you modify sheets(1).columns(1).specialcells(2) to encompass multiple columns ? & Can you choose a Non-Contiguous Range ? If so, HOW ?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]Sub M_snb()
    sn=Sheets("Sheet1").usedrange.resize(,3)

    With ListView1
    For j=1 to ubound(sn)
    if sn(j,1)<>"" then .ListItems.Add , , sn(j,1)
    for jj=2 to ubound(sn,2)
    .ListItems(j).ListSubItems.Add , , sn(j,jj)
    next
    Next
    End With
    end sub[/VBA]

  5. #5
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    snb:

    Haven't tested it yet, but will later.

    Thanks

  6. #6
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    snb :

    Got a chance to test it. It didn't work.
    I get a compile error at this point.
    [VBA]For j = 1 To UBound(sn)[/VBA]
    Compile Error; Expected Array

    I Have NOT used UBound extensively. Not sure how to fix it.

    Thanks

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I think 'sheet1' is empty.

  8. #8
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    snb:

    I was able to step through the code. This section:

    [VBA]For jj=2 To UBound(sn,2)[/VBA] and other array functions don't seem to like when they are blank cells or irregular formats in the data.
    I have a blank cell in the data range.
    I was trying to understand what the code was doing.( A learning experience ). Exactly where in your code can I tell it to start on Row 2 of my data rather than row 1, since I'm already giving it the headers that I want to use.

    Thanks for your info & assistance.

Posting Permissions

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