Listview从其他文件中检索数据

时间:2015-07-14 10:59:58

标签: excel-vba vba excel

我想从下面的路径而不是同一个文件中检索listview中的数据。您能否告知我的代码中需要进行哪些更改。

myFileNameDir = "C:\Users\GShaikh\Desktop\Book16.xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
Set ws1 = Worksheets("Students")



'Code retieving data from same file.

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 wksSource = Worksheets("Sheet1")

Set rngData = wksSource.Range("A1").CurrentRegion

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

RowCount = rngData.Rows.Count

ColCount = rngData.Columns.Count

    For i = 2 To RowCount
        Set LstItem = Me.ListView1.ListItems.Add(Text:=rngData(i, 1).Value)
        For j = 2 To ColCount
            LstItem.ListSubItems.Add Text:=rngData(i, j).Value
        Next j
    Next i

1 个答案:

答案 0 :(得分:0)

您可以从rngData范围向ListView添加数据,这里是您设置该范围的地方:

    Set rngData = wksSource.Range("A1").CurrentRegion

如果要使用打开的工作簿中的数据,则应修改rngData以引用该工作簿:

    Set rngData = ws1.Range("A1").CurrentRegion