PerryBeatty
03-23-2014, 09:12 AM
I've created a listview in one of my userforms and I've got 2 different sub routines to fill the listview. Both routines are accessing the same data set and userofrm, "ListRun" works but "ListFail" generates a type mismatch error. Can anyone help me in resolving this, I want to use the code in the "ListFail" sub routine.
Sub ListRun()
Dim LR As Long
Dim LC As Long
Dim i As Long
Dim cell As Range
Dim lsheet As Worksheet
Set lsheet = ThisWorkbook.Worksheets("List2")
lsheet.Select
With lsheet
LR = .Range("A" & .Rows.Count).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
form1.ListView1.ListItems.Clear
i = 1
With ListView1
For i = 1 To LR
Set cell = Cells(i, 1)
.ListItems.Add , , cell
.ListItems.item(i).ListSubItems.Add , , Cells(i, 2).Text
.ListItems.item(i).ListSubItems.Add , , Cells(i, 3).Text
.ListItems.item(i).ListSubItems.Add , , Cells(i, 4).Text
Next i
End With
End With
End Sub
Sub ListFail()
Dim item As ListItem
Dim LR As Integer
Dim i As Integer
Dim lsheet As Worksheet
Set lsheet = ThisWorkbook.Worksheets("List2")
ListView1.ListItems.Clear
LR = lsheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LR
Set item = ListView1.ListItems.Add(Text:=lsheet.Cells(i, 1)) <- THIS IS WHERE THE ERROR CODE HAPPENS
item.SubItems(1) = lsheet.Cells(i, 2)
item.SubItems(2) = lsheet.Cells(i, 3)
item.SubItems(3) = lsheet.Cells(i, 4)
Next
End Sub
Sub ListRun()
Dim LR As Long
Dim LC As Long
Dim i As Long
Dim cell As Range
Dim lsheet As Worksheet
Set lsheet = ThisWorkbook.Worksheets("List2")
lsheet.Select
With lsheet
LR = .Range("A" & .Rows.Count).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
form1.ListView1.ListItems.Clear
i = 1
With ListView1
For i = 1 To LR
Set cell = Cells(i, 1)
.ListItems.Add , , cell
.ListItems.item(i).ListSubItems.Add , , Cells(i, 2).Text
.ListItems.item(i).ListSubItems.Add , , Cells(i, 3).Text
.ListItems.item(i).ListSubItems.Add , , Cells(i, 4).Text
Next i
End With
End With
End Sub
Sub ListFail()
Dim item As ListItem
Dim LR As Integer
Dim i As Integer
Dim lsheet As Worksheet
Set lsheet = ThisWorkbook.Worksheets("List2")
ListView1.ListItems.Clear
LR = lsheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LR
Set item = ListView1.ListItems.Add(Text:=lsheet.Cells(i, 1)) <- THIS IS WHERE THE ERROR CODE HAPPENS
item.SubItems(1) = lsheet.Cells(i, 2)
item.SubItems(2) = lsheet.Cells(i, 3)
item.SubItems(3) = lsheet.Cells(i, 4)
Next
End Sub