Excel:使用VBA从其他Excel文件导入数据

68bkxrlz  于 2023-04-07  发布在  其他
关注(0)|答案(2)|浏览(349)

我想使用For循环一次性从10个其他Excel电子表格导入数据。这10个其他电子表格的名称列在工作表“文件夹”的A列中。
我录制了一个宏来捕获从一个名为TEST FILE的文件导入数据的步骤。如何更改此宏,使每个循环使用一个实际文件名,其中每个文件名存储为变量folderName
非常感谢。

Dim numFolders As Integer
Dim folderPosition As Integer
Dim folderName As String

numFolders = 10

For folderPosition = 1 To numFolders

folderName = Sheets("Folders").Range("A" & folderPosition).Value

ActiveWorkbook.Queries.Add Name:="TEST_FILE", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""C:\Location\TEST FILE.XLS""), null, true)," & Chr(13) & "" & Chr(10) & "    #""TEST_FILE1"" = Source{[Name=""TEST_FILE""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(#""TEST_FILE1"", [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Name"", type text}, {""Type""" & _
    ", type text}, {""Description"", type text}, {""Format"", type text}, {""Created"", type date}, {""Records"", type text}, {""List?"", type logical}, {""Created by"", type text}, {""Last run"", type date}, {""Last changed"", type date}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=2012_Appeals;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [TEST_FILE]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "_TEST_FILE"
    .Refresh BackgroundQuery:=False
End With
Application.CommandBars("Queries and Connections").Visible = False

Next
gpfsuwkq

gpfsuwkq1#

作为一个例子,这里是我的文件夹包含的文件(不要问我为什么使用xls....它只是什么是在文件夹中):

在我的工作簿中的工作表上,我有一个表格,列出了我感兴趣的文件:

创建仅连接查询以引用表:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"File List", type text}})
in
    ChangeType

接下来选择 * 数据〉获取数据〉从文件夹 *。这将显示该文件夹中所有文件的详细信息。
单击 * 转换数据 *

现在,您要将文件列表与文件夹中的文件列表合并。选择 * 合并查询 *,确定 * 名称 * 和 * 文件列表 * 列,然后单击确定。

通过按下列顶部的两个箭头来展开新列,并过滤掉空值,只保留表中的文件。

展开“内容”列,就像您对合并文件所做的那样。
选择您感兴趣的工作表或表格,然后按“确定”。

您的文件现在已合并并显示在表中:

现在,您可以使用 * 数据〉全部刷新 * 从文件中提取任何新数据,或将新文件添加到列表中以导入。

deikduxw

deikduxw2#

引用文件变量folderName和folderLocation,其中您已经硬编码了'TEST_FILE'和test_file location,如下所示:

Dim numFolders As Integer
Dim folderPosition As Integer
Dim folderName As String

numFolders = 10

For folderPosition = 1 To numFolders

folderName = Sheets("Folders").Range("A" & folderPosition).Value
folderLocation = "C:\Location\"+ folderName

ActiveWorkbook.Queries.Add Name:=folderName, Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""" + folderLocation + """), null, true)," & Chr(13) & "" & Chr(10) & "    #""TEST_FILE1"" = Source{[Name="""+ folderName + """]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(#""TEST_FILE1"", [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Name"", type text}, {""Type""" & _
    ", type text}, {""Description"", type text}, {""Format"", type text}, {""Created"", type date}, {""Records"", type text}, {""List?"", type logical}, {""Created by"", type text}, {""Last run"", type date}, {""Last changed"", type date}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=2012_Appeals;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [" + folderName + "]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "_"+ folderName 
    .Refresh BackgroundQuery:=False
End With
Application.CommandBars("Queries and Connections").Visible = False

Next

相关问题