• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to collate all files from one folder in single file

ThrottleWorks

Excel Ninja
Hi,

I have a folder. In this folder, there will be approx. 50 files.

I want to collate all these files in one new file.

Data is present only in sheet 1 of all the files. Collated file will have a single row of headers from original files.

User will select this folder by browsing option provided in the macro.

How can I do it, can anyone please help me in this.
 
Hi @ThrottleWorks,

Something like this should work... it was made for columns A:D but can be changed to fit the exact range in question.
As it is, you just need to paste the code into the workbook you wish to copy the data to and you should be good to go.
I didn't transfer the headers from the files so you will need to fill the first row (either before or after running the code)
Code:
Sub OpenFiles()

    Application.ScreenUpdating = False
   
        Dim MyFolder As String
        Dim MyFile As String
        Dim wrkbk As String
       
        wrkbk = ActiveWorkbook.Name
       
        On Error Resume Next
        With Workbooks(wrkbk).Sheets(1)
            .Range("A2:D" & .Range("A2").End(xlDown).Row).ClearContents
        End With
       
        MyFolder = GetFolder("")
        MyFile = Dir(MyFolder & "\*.xlsx")
       
        Do While MyFile <> ""
            Workbooks.Open Filename:=MyFolder & "\" & MyFile
            MyFile = Dir
           
            With ActiveWorkbook.Sheets(1)
                Range("A2:D" & Range("A2").End(xlDown).Row).Copy Workbooks(wrkbk).Sheets(1).Cells(Workbooks(wrkbk).Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
                ActiveWorkbook.Close savechanges:=False
            End With
        Loop
   
    Application.ScreenUpdating = True

End Sub

Function GetFolder(strPath As String) As String

    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
   
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With

NextCode:
    GetFolder = sItem
    Set fldr = Nothing

End Function

Hope it helps

Regards :)
 
Back
Top