VBA code to merge data from files into one master file

onche414

New Member
Joined
Aug 7, 2023
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have multiple excel files in a folder and I would like to have a code which will go through that folder and copy as value the data (from column A to Z) from the first sheet of each of the excel files and gather them in a master data file in one sheet (i.e copy data from excel file 1 then copy below the data from excel file 2, etc)

Thank you,
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Perhaps this gets you started?
VBA Code:
Sub ConsolidateFiles()
    Dim lCount As Long
    Dim vFileName As Variant
    Dim sPath As String
    Dim lFilecount As Long
    sPath = "c:\windows\temp\"
    ChDrive sPath
    ChDir sPath
    vFileName = _
        Application.GetOpenFilename("Microsoft Excel files (*.xls*),*.xls*", _
        , "Please select the file(s) to consolidate", , True)
    If TypeName(vFileName) = "Boolean" Then Exit Sub
    For lCount = LBound(vFileName) To UBound(vFileName)
        ProcessFile CStr(vFileName(lCount))
    Next
End Sub


Sub ProcessFile(sFileName As String)
    Dim oSh As Worksheet
    Workbooks.Open sFileName
    For Each oSh In Worksheets
        oSh.UsedRange.Copy
        With ThisWorkbook.Worksheets("AllData")
            .Range("c" & _
                .Rows.Count).End(xlUp).Offset(1).PasteSpecial _
                xlPasteValuesAndNumberFormats
            .Range(.Range("A" & .Rows.Count).End(xlUp).Offset(1), _
                .Range("C" & .Rows.Count).End(xlUp).Offset(0, -2)).Value _
                = ActiveWorkbook.FullName
            .Range(.Range("B" & .Rows.Count).End(xlUp).Offset(1), _
                .Range("C" & .Rows.Count).End(xlUp).Offset(0, -1)).Value _
                = oSh.Name
        End With
    Next
    Application.CutCopyMode = False
    ActiveWorkbook.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top