+ Reply to Thread
Results 1 to 5 of 5

Macro to combine multiple (small) files into one file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-08-2015
    Location
    NJ
    MS-Off Ver
    2013
    Posts
    205

    Macro to combine multiple (small) files into one file

    Hello, curious on a macro that would potentially bring information together on one spreadsheet. There will be a few hundred small spreadsheets, all exactly the same, that we need to bring together into one list, to essentially be a data dump of all information. This includes each single file as it's own line on the final spreadsheet. I attached four spreadsheets that mirror the single files, along with a final file, which is the desired result.

    Each of the single files would be located on a shared drive in the same folder location. Any suggestions on how to make this work?

    Thanks so much.
    Sheryl
    Attached Files Attached Files

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Macro to combine multiple (small) files into one file

    This will work. Change the code highlighted in red. Path to files. Also sheet name, I'm assuming it'll be named sheet1. Also assumes these will be the only files in the folder.

    Sub ImportSheet()
     Const MGR_NAME As String = "A2"
     Const EMP_NAME As String = "C2"
     Const QUESTIONS As String = "A5"
        Dim i As Long
        Dim SourceFolder As String
        Dim FileList As Variant
        Dim GrabSheet As String
        Dim FileType As String
        Dim ActWorkBk As String
        Dim ImpWorkBk As String
        Dim NoImport As Boolean
    
        
        'Define folder location (and filetypes)
        SourceFolder = "C:\Users\Mike\Downloads\Test"
        FileType = "*.xlsx"
    
        'Define sheetname to copy
        GrabSheet = "Sheet1"
    
        'Creates list with filenames
        FileList = ListFiles(SourceFolder & "/" & FileType)
    
        'Imports data
        Application.ScreenUpdating = False
        ActWorkBk = ActiveWorkbook.Name
        NoImport = False
    
        For i = 1 To UBound(FileList)
            'Opens file
            Workbooks.Open (SourceFolder & "\" & FileList(i))
            ImpWorkBk = ActiveWorkbook.Name
    
            'Checks to see if the specific sheet exists in the workbook
            On Error Resume Next
                ActiveWorkbook.Sheets(GrabSheet).Select
                If Err.Number > 0 Then
                    NoImport = True
                    GoTo nxt
                End If
                Err.Clear
            On Error GoTo 0
    
            'Copies questions
            With Workbooks(ActWorkBk).ActiveSheet.Cells(Rows.Count, 1).End(xlUp)
                .Offset(1) = Workbooks(ImpWorkBk).Sheets(GrabSheet).Range(MGR_NAME).Value
                .Offset(1, 1) = Workbooks(ImpWorkBk).Sheets(GrabSheet).Range(EMP_NAME).Value
                .Offset(1, 2).Resize(, 7) = Workbooks(ImpWorkBk).Sheets(GrabSheet).Range(QUESTIONS).Resize(, 7).Value
            End With
            On Error Resume Next
                Err.Clear
            On Error GoTo 0
    
    nxt:
            'Closes importfile
            Workbooks(ImpWorkBk).Activate
            Application.DisplayAlerts = False
            ActiveWorkbook.Saved = True
            ActiveWorkbook.Close SaveChanges:=False
            Application.DisplayAlerts = True
            Workbooks(ActWorkBk).Activate
    
        Next i
    
        'Error if some sheets were not found
        If NoImport = True Then MsgBox "One or more sheets could not be found and imported!"
    
        Application.ScreenUpdating = True
    End Sub
    
    
    'Function that creates an array with all the files in the folder
    Function ListFiles(Source As String) As Variant
        Dim GetFileNames() As Variant
        Dim i As Integer
        Dim FileName As String
    
        On Error GoTo ErrHndlr
    
        i = 0
        FileName = Dir(Source)
        If FileName = "" Then GoTo ErrHndlr
    
        'Loops until no more mathing files are found
        Do While FileName <> ""
            i = i + 1
            ReDim Preserve GetFileNames(1 To i)
            GetFileNames(i) = FileName
            FileName = Dir()
        Loop
        ListFiles = GetFileNames
        On Error GoTo 0
        Exit Function
    
        'If error
    ErrHndlr:
        ListFiles = False
        On Error GoTo 0
    End Function
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Macro to combine multiple (small) files into one file

    Try this - just change myPath to the folder where the individual files are kept:

    Sub CopyDataFromFiles()
    
    Dim wbM, wb As Workbook
    Dim myPath, myFile, myExt As String
    Dim nr, lr As String
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    Set wbM = ThisWorkbook
    nr = wbM.Sheets(1).Range("A" & wbM.Sheets(1).Rows.Count).End(xlUp).Row + 1
    myPath = "C:\temp\Data Files\"
    myExt = "*.xls*"
    myFile = Dir(myPath & myExtension)
    
    Do While myFile <> ""
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        wbM.Sheets(1).Range("A" & nr).Value = wb.Sheets(1).Range("A2")
        wbM.Sheets(1).Range("B" & nr).Value = wb.Sheets(1).Range("C2")
        wb.Sheets(1).Range("A5:G5").Copy Destination:=wbM.Sheets(1).Range("C" & nr)
        wb.Close SaveChanges:=False
        nr = nr + 1
        myFile = Dir
    Loop
    
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    Regards,
    Stephen

    If you feel someone has helped you please thank them and click on the star on their post to add reputation.
    Please ensure that you use code tags where relevant, and mark solved threads as solved.
    Most of all please be respectful and understanding of others.

  4. #4
    Forum Contributor
    Join Date
    08-08-2015
    Location
    NJ
    MS-Off Ver
    2013
    Posts
    205

    Re: Macro to combine multiple (small) files into one file

    Fantastic!!! Thanks so much! Wow those were quick. Appreciate the help, this will be very helpful as we might not have the appropriate technology to support this effort, so this gives us an alternative solution.

    Thank you again,
    Sheryl

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Another way !


    Hi Sheryl !

    Next demonstration code must be in final file worksheet class module
    and workbook saved as .xlsb or .xlsm in same single files folder :

    PHP Code: 
    Sub Demo1()
        Const 
    ";Extended Properties=""Excel 12.0;HDR=No"""
        
    Dim oCn As ObjectP$, F$, R&, V
              P 
    ThisWorkbook.Path "\"
              F = Dir(P & "
    *.xlsx"):  If F = "" Then Beep: Exit Sub
        Me.UsedRange.Offset(1).Clear
        [E2].Value = "     
    Wait …"
        Application.ScreenUpdating = False
        Set oCn = CreateObject("
    ADODB.Connection")
              P = "
    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & P
              R = 1
        Do
                 oCn.Open P & F & E
            With oCn.Execute("
    SELECT FROM [A2:G5]")
                V = .GetRows
                    .Close
            End With
                 oCn.Close
              R = R + 1
              Cells(R, 1).Resize(, 9).Value = Array(V(0, 0), V(2, 0), V(0, 3), V(1, 3), V(2, 3), V(3, 3), V(4, 3), V(5, 3), V(6, 3))
              F = Dir
        Loop Until F = ""
        Set oCn = Nothing
        Application.ScreenUpdating = True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 04-09-2017 at 01:39 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Combine data from multiple files into a master file using VBA
    By bdrod in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-18-2016, 06:00 PM
  2. [SOLVED] New to Excel programming. In need of a macro to combine multiple files with multiple tabs.
    By nflores0303 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-12-2016, 11:03 AM
  3. Replies: 4
    Last Post: 10-04-2015, 08:21 AM
  4. Macro to combine same-named sheets from multiple files
    By virsilens in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-13-2014, 12:05 AM
  5. Macro to combine and clean multiple files
    By micahjb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2013, 07:16 PM
  6. Combine multiple Ecxel files in one master file with VB
    By jelena1290 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-10-2012, 12:36 AM
  7. Replies: 4
    Last Post: 09-22-2010, 01:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1