A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi Debangshu,
there are so much errors in your code... please spare me.
Andreas.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
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.
Hi Debangshu,
there are so much errors in your code... please spare me.
Andreas.
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
Hi Andreas,
Can you please let me know about the error in my code?
Thanks,
Debangshu