loop files one folder

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Loop files in one folder

Loop files in one folder

resolvedResolved · High Priority · Version 2010

Freddie has attended:
Excel VBA Introduction course

Loop files in one folder

Hi Guys

In an effort to reduced copying and pasting, I want to create a macro that goes through all working day files from the previous in a folder and does the below for each file (i.e. copies the data from file A to the master file then closes file A then onto file B ).

All I have is the below...

Workbooks.Open Filename:="Y:\2017\201705\6115\20170504_cd_6115.xls"
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("FTP MAY FILE.xls").Activate
Range("A149").Select
ActiveSheet.Paste
Windows("20170504_cd_6115.xls").Activate
ActiveWindow.Close
Workbooks.Open Filename:="Y:\2017\201705\6115\20170505_cd_6115.xls"
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("FTP MAY FILE.xls").Activate

Please help!

Thanks

Freddie

RE: Loop files in one folder

Hi Freddie,

Thank you for the forum question.

I am using a folder picker. If you use the code below, Excel will prompt you for a folder. Click on the folder and click ok and the code will loop through all Excel files in the folder.

Sub LoopAll()

Dim FolderPath As String
Dim FileName As String
Dim WorkBk As Workbook



'speed up macro
Application.ScreenUpdating = False
Application.DisplayAlerts = False


'open the select directory dialog box
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then '-1 = yes or true
FolderPath = .SelectedItems(1) & "\"
Else
MsgBox "FilePath not selected!", , "Path selecter"
Exit Sub
End If

End With




' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")

' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & "\" & FileName)


‘insert your code here


' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False

' Use Dir to get the next file name.
FileName = Dir()
Loop



Application.ScreenUpdating = true
Application.DisplayAlerts = true


End Sub


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Loop files in one folder

Hi Jens,

Thanks for the above.

The only problem I now have is copying the data from each workbook to the master file.

How do I "dim" each workbook from the folder?

Thanks

Freddie

RE: Loop files in one folder

Hi Freddie,

I have used an array instead of copy and paste. THis is much faster and better in VBA. Stay away from copy and paste in your code. It will give you a lot of problems.

The code will take all records from sheet one from all the workbooks in the folder you select. You will need to change the name of the destination sheet in the code. In my code all lists start from row 1. If you tables/lists start from another row, you will have to change the code.


Sub MergeAllWorkbooks()
'variable to store information in the computer's memory

Dim FolderPath As String 'store the folderpath in the computer's memory
Dim FileName As String 'store the file name inthe computer's memory
Dim WorkBk As Workbook 'store the name of each workbook in the computer's memory
Dim blankrow As Long 'store the row number of the first blank row in destination sheet
Dim varAllData As Variant 'array. Will store all records from the source workbooks
'error handling. If there is an error Excel will go to the bottom of the macro where the error name is
'On Error GoTo ErrHandler





'speed up macro. Stops Excel from updating screen while macro is running
Application.ScreenUpdating = False
'stops Excel from showing dialog boxes while running macro.
Application.DisplayAlerts = False


'open the select directory dialog box
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False

'"IF THEN ELSE" decision code. If folder is selected then store the folder path in the variable folderpath

If .Show = -1 Then '-1 = yes or true
FolderPath = .SelectedItems(1) & "\"

'if folder is not selected the user will get a mesaage box and Excel will exit the macro

Else
MsgBox "FilePath not selected!", , "Path selecter"
Exit Sub
End If

End With




' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")

' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
'add all data from eack workbook to the array
varAllData = WorkBk.Sheets(1).Range("a1").CurrentRegion.Offset(1, 0)
'will select the right destination sheet
ThisWorkbook.Sheets("Destination").Activate
'find the first blank row
blankrow = Range("a1").CurrentRegion.Rows.Count + 1
'"Empty" the array onto destination starting from column A and will get the right row number from the variable blankrow
'resize the destination for the data stored in the array varAllData
Range("A" & blankrow).Resize(UBound(varAllData, 1), UBound(varAllData, 2)) = varAllData

' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False

' Use Dir to get the next file name.
FileName = Dir()
'end of loop

Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub









Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Removing border lines on the keyboard

Highlight your cell(s) that have boreders on them and press CTRL + SHIFT + _, this will then remove the border lines.

View all Excel hints and tips


Server loaded in 0.05 secs.