Share via

Userform ListView Control

Anonymous
2019-08-08T08:31:21+00:00

Hi,

I am using the below code to populate data from data range to ListView. I have data range with 2 nos of columns. In the ListView column 1 and column 2 showing the same data. It is not able to populate column 2 data to ListView.

Please help to fix this issue.

Private Sub UserForm_Initialize()

Dim rngcell, myrange As Range

Dim lastrow_1 As Integer

Me.ListView1.ListItems.Clear

Me.ListView1.ColumnHeaders.Clear

lastrow_1 = Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row

Me.ListView1.Width = 180

Me.Width = 240

Set myrange = Sheets("Sheet1").Range("B1" & ":D" & lastrow_1)

colcount = myrange.Columns.Count

'MsgBox colcount

For a = 1 To 2

Me.ListView1.ColumnHeaders.Add Width:=50

Next a

For i = 1 To lastrow_1

    Set ListItem = Me.ListView1.ListItems.Add(Text:=myrange(i, 1).Value)

    'MsgBox myrange(i, 1)

        For j = 1 To colcount

             ListItem.ListSubItems.Add Text:=myrange(i, j).Value

             'MsgBox myrange(i, j)

        Next j

    Next i

'ListView1.ListItems.Item(lastrow_1).Selected = True

Me.ComboBox1.AddItem "600"

Me.ComboBox1.AddItem "800"

Me.ComboBox1.AddItem "1200"

End Sub

Thanks,

Debangshu

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2019-08-08T09:51:15+00:00

    Hi Debangshu,

    there are so much errors in your code... please spare me.

    Andreas.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2019-08-08T09:17:40+00:00

    Try the code below.

    Andreas.

    Private Sub UserForm_Initialize()
      Const Space = 3
      Dim Where As Range
      Dim Data
      Dim i As Long, j As Long
      Dim LItem As MSComctlLib.ListItem
      
      'Refer to all data in this columns
      Set Where = Intersect(Range("B:C"), Range("B1").CurrentRegion)
      'Read in all values
      Data = Where.Value
      With Me.ListView1
        'Prepare
        .View = lvwReport
        .Gridlines = True
        'Add the headings and use the column width of the sheet + a little space
        For j = 1 To UBound(Data, 2)
          .ColumnHeaders.Add Text:=Data(1, j), Width:=Where(1, j).Width + Space
        Next
        
        For i = 2 To UBound(Data)
          'Add a row and store the first item
          Set LItem = Me.ListView1.ListItems.Add(Text:=Data(i, 1))
          'Store the rest of this row
          For j = 2 To UBound(Data, 2)
            LItem.SubItems(j - 1) = Data(i, j)
          Next
        Next
      End With
    End Sub
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2019-08-08T09:36:30+00:00

    Hi Andreas,

    Can you please let me know about the error in my code?

    Thanks,

    Debangshu

    Was this answer helpful?

    0 comments No comments