Need VBA code: Merge all Excel files in a floder to a new excel Sheet

Veenu90

New Member
Joined
Aug 13, 2014
Messages
5
HI,

I am a beginner in Excel and need help in writing a VBA code. We work in Excel 2010 and every week I need to prepare a consolidated excel file after merging all the Shared excel files in a folder. I do it manually by copy paste and it takes lots of time and effort. Could someone help me to do it via VBA code. Things that I need are:

1: All data is in Sheet1 of every Excel workbook
2: Row 1 contains the column names in every excel file so I need that the hadings are being copied to consolidated sheet only once and from other files in folder the data is being copied from Row 2. (If Possible)
2: As we work on a shared excel file so sometimes it has filters applied on columns as well, so i need to remove filters as well before I copy the rows.

Thanks for your time.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Before running the macro, you'll first need to change this line to match the folder containing your workbooks:

Code:
FileFold = "C:\Users\jsmith\Desktop"


The code is as follows. You don't need to have any of your workbooks open in order to run this.

Code:
Sub Test()

    Dim FileFold As String
    Dim FileSpec As String
    Dim FileName As String
    Dim ShtCnt As Long
    Dim RowCnt As Long
    Dim Merged As Workbook
    Dim wb As Workbook
    Dim ws As Worksheet
    
    FileFold = "C:\Users\jsmith\Desktop" 'set the folder
    
    FileSpec = FileFold & Application.PathSeparator & "*.xl*"
    FileName = Dir(FileSpec)
    
    'Exit if no files found
    If FileName = vbNullString Then
        MsgBox Prompt:="No files were found that match " & FileSpec, Buttons:=vbCritical, Title:="Error"
        Exit Sub
    End If
    
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    
    ShtCnt = 0
    RowCnt = 1
    
    Set Merged = Workbooks.Add
    
    Do While FileName <> vbNullString
        ShtCnt = ShtCnt + 1
        Set wb = Workbooks.Open(FileName:=FileFold & Application.PathSeparator & FileName, UpdateLinks:=False)
        Set ws = wb.Worksheets("Sheet1")
        With ws
            If .FilterMode Then .ShowAllData
            If ShtCnt > 1 Then .Rows(1).EntireRow.Delete Shift:=xlUp
            .Range("A1").CurrentRegion.Copy Destination:=Merged.Worksheets(1).Cells(RowCnt, 1)
        End With
        wb.Close SaveChanges:=False
        RowCnt = Application.WorksheetFunction.CountA(Merged.Worksheets(1).Columns("A:A")) + 1
        FileName = Dir
    Loop
    
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
    
    MsgBox Prompt:="Finished merging.", Buttons:=vbInformation, Title:="Success"

End Sub
 
Upvote 0
Dear gpeacock,
I have used your macro and it is very helpful. However, I am finding an issue that I do not know how to solve and wondered if you can assist. I am a novice at VBA.
I am running the macro on a folder with files of different extensions, so I changed the code. Instead of "*.xl*" I am using "*.x*"
It is compiling all files correctly.
I would like to confirm if if the macro is meant to show all content. Will it unfilter, unhide and unfreeze contents in the files before merging them?
The problem I am finding is: although it is combining all files, SOME of the content from the originals, in the last 3 or 4 columns, is not being included when the files are combined.
I am puzzled because most of the time all the content is compiled correctly and only in a few cases it is not. I have not been able to identify any reason why.
Would you kindly be able to assist?
Many thanks in advance.
 
Upvote 0
Hi gpeacock, How can I combine first row?
Before running the macro, you'll first need to change this line to match the folder containing your workbooks:

Code:
FileFold = "C:\Users\jsmith\Desktop"


The code is as follows. You don't need to have any of your workbooks open in order to run this.

Code:
Sub Test()

    Dim FileFold As String
    Dim FileSpec As String
    Dim FileName As String
    Dim ShtCnt As Long
    Dim RowCnt As Long
    Dim Merged As Workbook
    Dim wb As Workbook
    Dim ws As Worksheet
    
    FileFold = "C:\Users\jsmith\Desktop" 'set the folder
    
    FileSpec = FileFold & Application.PathSeparator & "*.xl*"
    FileName = Dir(FileSpec)
    
    'Exit if no files found
    If FileName = vbNullString Then
        MsgBox Prompt:="No files were found that match " & FileSpec, Buttons:=vbCritical, Title:="Error"
        Exit Sub
    End If
    
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    
    ShtCnt = 0
    RowCnt = 1
    
    Set Merged = Workbooks.Add
    
    Do While FileName <> vbNullString
        ShtCnt = ShtCnt + 1
        Set wb = Workbooks.Open(FileName:=FileFold & Application.PathSeparator & FileName, UpdateLinks:=False)
        Set ws = wb.Worksheets("Sheet1")
        With ws
            If .FilterMode Then .ShowAllData
            If ShtCnt > 1 Then .Rows(1).EntireRow.Delete Shift:=xlUp
            .Range("A1").CurrentRegion.Copy Destination:=Merged.Worksheets(1).Cells(RowCnt, 1)
        End With
        wb.Close SaveChanges:=False
        RowCnt = Application.WorksheetFunction.CountA(Merged.Worksheets(1).Columns("A:A")) + 1
        FileName = Dir
    Loop
    
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
    
    MsgBox Prompt:="Finished merging.", Buttons:=vbInformation, Title:="Success"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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