列表视图从其他文件中检索数据。

问题描述 投票:0回答:1

我想从下面的路径而不是同一个文件中检索列表视图中的数据。请你告诉我,我的代码需要做哪些改动。

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

'Code for retrieving 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
excel-vba vba excel
1个回答
0
投票

你从rngData Range中添加数据到ListView中,这里是你设置Range的地方。

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

如果你想使用你打开的工作簿中的数据, 你应该修改rngData来代替引用那个工作簿。

    Set rngData = ws1.Range("A1").CurrentRegion
© www.soinside.com 2019 - 2024. All rights reserved.