Hi All
I have been googling to see if I could solve this but cannot find a solution
My problem is I have this code that loads all xls files from a folder to one worksheet in a workbook. windows 10 File Explorer shows files listed by date order.
When I run the code excel, loads file data in this order.
Link to the code I’m using
Loop Through All Excel Files In A Given Folder — The Spreadsheet Guru
I'd be much appreciated if anyone has a solution
Thanks in advance
Graham
I have been googling to see if I could solve this but cannot find a solution
My problem is I have this code that loads all xls files from a folder to one worksheet in a workbook. windows 10 File Explorer shows files listed by date order.
C:\\TEMP\results_2013-1-1.xls |
C:\\TEMP\results_2013-1-2.xls |
C:\\TEMP\results_2013-1-3.xls |
C:\\TEMP\results_2013-1-4.xls |
C:\\TEMP\results_2013-1-5.xls |
C:\\TEMP\results_2013-1-6.xls |
C:\\TEMP\results_2013-1-7.xls |
C:\\TEMP\results_2013-1-8.xls |
C:\\TEMP\results_2013-1-9.xls |
C:\\TEMP\results_2013-1-10.xls |
C:\\TEMP\results_2013-1-11.xls |
C:\\TEMP\results_2013-1-12.xls |
C:\\TEMP\results_2013-1-13.xls |
C:\\TEMP\results_2013-1-14.xls |
C:\\TEMP\results_2013-1-15.xls |
C:\\TEMP\results_2013-1-16.xls |
C:\\TEMP\results_2013-1-17.xls |
C:\\TEMP\results_2013-1-18.xls |
C:\\TEMP\results_2013-1-19.xls |
C:\\TEMP\results_2013-1-20.xls |
C:\\TEMP\results_2013-1-21.xls |
C:\\TEMP\results_2013-1-22.xls |
C:\\TEMP\results_2013-1-23.xls |
C:\\TEMP\results_2013-1-24.xls |
C:\\TEMP\results_2013-1-25.xls |
C:\\TEMP\results_2013-1-26.xls |
C:\\TEMP\results_2013-1-27.xls |
C:\\TEMP\results_2013-1-28.xls |
C:\\TEMP\results_2013-1-29.xls |
C:\\TEMP\results_2013-1-30.xls |
C:\\TEMP\results_2013-1-31.xls |
VBA Code:
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: [URL='http://www.TheSpreadsheetGuru.com']www.TheSpreadsheetGuru.com[/URL]
Sheets("Results").Select
Dim WB As Workbook
Dim MyPath As String
Dim MyFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
MyPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
MyPath = MyPath
If MyPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
MyFile = Dir(MyPath & myExtension)
'Loop through each Excel file in folder
Do While MyFile <> ""
'Set variable equal to opened workbook
Set WB = Workbooks.Open(Filename:=MyPath & MyFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
Dim w As Workbook
Set w = ActiveWorkbook
' 'Do other stuff
Cells.Select
ActiveWindow.Zoom = 75
Selection.Columns.AutoFit
ActiveCell.Select
Application.Goto Reference:="R2C1"
ActiveCell.Range("A1:AT1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
DoEvents
Windows("COURSE Results.xlsm").Activate
Sheets("Results").Select
DoEvents
Range("A" & Cells.Rows.Count).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Application.ScreenUpdating = True
Windows("COURSE Results.xlsm").Activate
ActiveSheet.Paste
w.Activate
Application.DisplayAlerts = False
'do the deletions here
'Save and Close Workbook
WB.Close SaveChanges:=False
'Ensure Workbook has closed before moving on to next line of code
DoEvents
Application.DisplayAlerts = True
'Get next file name
MyFile = Dir
Loop
'Message Box when tasks are completed
' MsgBox "Task Complete!"
Range("A" & Cells.Rows.Count).End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ResetSettings:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
When I run the code excel, loads file data in this order.
C:\\TEMP\results_2013-1-1.xls |
C:\\TEMP\results_2013-1-10.xls |
C:\\TEMP\results_2013-1-11.xls |
C:\\TEMP\results_2013-1-12.xls |
C:\\TEMP\results_2013-1-13.xls |
C:\\TEMP\results_2013-1-14.xls |
C:\\TEMP\results_2013-1-15.xls |
C:\\TEMP\results_2013-1-16.xls |
C:\\TEMP\results_2013-1-17.xls |
C:\\TEMP\results_2013-1-18.xls |
C:\\TEMP\results_2013-1-19.xls |
C:\\TEMP\results_2013-1-2.xls |
C:\\TEMP\results_2013-1-21.xls |
C:\\TEMP\results_2013-1-22.xls |
C:\\TEMP\results_2013-1-23.xls |
C:\\TEMP\results_2013-1-24.xls |
C:\\TEMP\results_2013-1-25.xls |
C:\\TEMP\results_2013-1-26.xls |
C:\\TEMP\results_2013-1-27.xls |
C:\\TEMP\results_2013-1-28.xls |
C:\\TEMP\results_2013-1-29.xls |
C:\\TEMP\results_2013-1-3.xls |
C:\\TEMP\results_2013-1-30.xls |
C:\\TEMP\results_2013-1-31.xls |
C:\\TEMP\results_2013-1-4.xls |
C:\\TEMP\results_2013-1-5.xls |
C:\\TEMP\results_2013-1-6.xls |
C:\\TEMP\results_2013-1-7.xls |
C:\\TEMP\results_2013-1-8.xls |
C:\\TEMP\results_2013-1-9.xls |
Link to the code I’m using
Loop Through All Excel Files In A Given Folder — The Spreadsheet Guru
I'd be much appreciated if anyone has a solution
Thanks in advance
Graham
Last edited by a moderator: