PDA

View Full Version : [SOLVED:] ListView control problem



lifeson
12-11-2007, 12:33 PM
I am trying to modify some code that I have used to populate a "listbox" (thanks Rory) that I am trying to amend to populate a "listview" control instead.
This is what I have got:


Private Sub UserForm_Initialize()
With ListView1
With .ColumnHeader
.Clear
.Add , , "ID", 20
.Add , , "FileName", 150
.Add , , "FileType", 60
.Add , , "Version"
End With
End With
Call ListDocs
End Sub

Private Sub ListDocs()
Dim lrow As Long
Dim aryDocs
Dim i As Long
Dim idx As String
Dim fileName As String
Dim fileType As String
Dim fileDate As Date
'which sheet to use
Sheets("BlrDoc").Select
'count how many rows to search
lrow = Cells(rows.Count, "A").End(xlUp).row
idx = "BLR1111"
x = x + 1
With ListView1
ReDim aryDocs(1 To lrow)
For i = 2 To lrow
If Cells(i, "A").Value = idx Then
.ListItems.Add , , Cells(i, "B").Value
fileName = Application.VLookup(Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 2, False)
fileType = Application.VLookup(Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 3, False)
fileDate = Application.VLookup(Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 4, False)
.ListItems(x).ListSubItems.Add , , fileName
.ListItems(x).ListSubItems.Add , , fileType
.ListItems(x).ListSubItems.Add , , Format(fileDate, "mm/yy")
End If
Next I
End With
End Sub

It works fine for the first column of the listview correctly showing all the docs associated with BLR1111 :thumb , and the first row is correct showing the properties of the document, but the subsequent rows do not show the document properties for the doc id in the first row.:(

I think this bit is wrong :think:


.ListItems(x).ListSubItems.Add , , fileName
.ListItems(x).ListSubItems.Add , , fileType
.ListItems(x).ListSubItems.Add , , Format(fileDate, "mm/yy")

Any one familiar with the lsitview control properties? :dunno

carlmack
12-11-2007, 01:53 PM
I think you just need to add x = x+1 in the loop.

It might be a good idea to get rid of that select as well.



Private Sub ListDocs()
Dim lrow As Long
Dim aryDocs() As String
Dim i As Long
Dim idx As String
Dim fileName As String
Dim fileType As String
Dim fileDate As Date
Dim ws As Worksheet
Dim wsdoc As Worksheet
Dim x As Long
Set ws = ThisWorkbook.Worksheets("BlrDoc")
lrow = ws.Cells(Rows.Count, "A").End(xlUp).row
x = 1
idx = "BLR1111"
With ListView1
ReDim aryDocs(1 To lrow)
For i = 2 To lrow
If ws.Cells(i, "A").Value = idx Then
.ListItems.Add , , ws.Cells(i, "B").Value
fileName = Application.VLookup(ws.Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 2, False)
fileType = Application.VLookup(ws.Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 3, False)
fileDate = Application.VLookup(ws.Cells(i, "B").Value, Worksheets("docs").Columns("A:E"), 4, False)

.ListItems(x).ListSubItems.Add , , fileName
.ListItems(x).ListSubItems.Add , , fileType
.ListItems(x).ListSubItems.Add , , Format(fileDate, "mm/yy")
x = x + 1
End If
Next i
End With
End Sub


HTH
Carl

idxy
04-29-2008, 01:43 PM
Thanks.