填充userform listview

时间:2014-05-04 12:48:13

标签: excel-vba vba excel

我正在尝试从Sheet1

中的范围填充用户表单中的列表视图

这是我正在使用的代码

Private Sub UserForm_Activate()
'Set some of the properties for the ListView
With Me.ListView1
    .HideColumnHeaders = False
    .View = lvwReport
End With

'Declare the variables
Dim wksSource As Worksheet
Dim rngData As Range
Dim rngCell As Range
Dim LstItem As ListItem
Dim RowCount As Long
Dim ColCount As Long
Dim i As Long
Dim j As Long

'Set the source worksheet
Set wksSource = Worksheets("Sheet1")

'Set the source range
Set rngData = wksSource.Range("A1").CurrentRegion

'Add the column headers
For Each rngCell In rngData.Rows(1).Cells
    Me.ListView1.ColumnHeaders.Add Text:=rngCell.Value, Width:=90
Next rngCell

'Count the number of rows in the source range
RowCount = rngData.Rows.Count

'Count the number of columns in the source range
ColCount = rngData.Columns.Count

'Fill the ListView
For i = 2 To RowCount
    Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value) '==> Error here
    For j = 2 To ColCount
        LstItem.ListSubItems.Add Text:=rngData(i, j).Value
    Next j
Next i
End Sub

但问题是我总是得到这个错误

运行时错误'13': 类型不匹配

任何帮助PLZ?

提前感谢你

2 个答案:

答案 0 :(得分:0)

替换:

Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)

使用:

Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Text)

答案 1 :(得分:0)

替换: 设置LstItem = Me.ListView1.ListItems.Add(文本:= rngData(i,1).Value)

使用: Me.ListView1.ListItems.Add(Text:= rngData(i,1).Value)

尝试删除“Set LstItem =”