I want to use a For loop to simultaneously import data from ten different Excel spreadsheets. Column A of the sheet "Folders" lists the names of these ten more spreadsheets.
To document the procedures involved in importing data from a file called TEST FILE, I created a macro. How can I modify this so that each loop uses a different file name, each of which is saved in the variable 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