vba 填充用户表单列表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23456403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate userform listview
提问by user3286479
I'm trying to fill a listview in a userform from a range in Sheet1
我正在尝试从 Sheet1 中的范围填充用户表单中的列表视图
This is the code I'm using
这是我正在使用的代码
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
But the problem is that I always get this error
但问题是我总是收到这个错误
Run-time error '13': Type mismatch
运行时错误“13”:类型不匹配
Any help plz ?
有什么帮助吗?
Thank u in advance
提前谢谢你
回答by Gary's Student
Replace:
代替:
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
with:
和:
Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Text)
回答by Chad Luton
Replace: Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
替换:设置 LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
With: Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
与: Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
Try removing "Set LstItem = "
尝试删除“Set LstItem =”