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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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