Auto-merging several xls into one

maxon

New Member
Joined
Oct 28, 2015
Messages
43
Hello,

I need your advice, in many vba project I have to merge several xls files into one. Im using almost always following code (below). What do you think about it? Is it efficient or maybe it could be done better and easier? I would be grateful if you could provide me examples how it will be better.

Code:
'count no of reports
Filename = Dir(plik & Filtr)
i = 0
Do Until Filename = ""
    ReDim Preserve Filelist(i)
    Filelist(i) = Filename
    Filename = Dir
    i = i + 1
Loop

'copy data from first report with header
Application.ScreenUpdating = False

K = 0
Workbooks.Open (plik & Filelist(K))

ActiveWorkbook.Sheets(1).Range("A1").CurrentRegion.Copy wbINC.Range("A1")
ActiveWorkbook.Close False

'copy data from other reports
If i > 0 Then
    For i = 1 To UBound(Filelist)
        Workbooks.Open (plik & Filelist(i))
        MacroLastRow = wbINC.Range("A1").CurrentRegion.Rows.count
        ReportRows = ActiveWorkbook.Sheets(1).Range("A1").CurrentRegion.Rows.count

        If ReportRows > 1 Then
            With ActiveWorkbook.Sheets(1).Range("A1").CurrentRegion
                .Offset(1, 0).Resize(.Rows.count - 1).Rows.Copy wbINC.Range("A" & MacroLastRow + 1)
            End With
            ActiveWorkbook.Close False
        Else
        ActiveWorkbook.Close False
        GoTo nextstep:
        End If
nextstep:
    Next
End If

Thank you in advance,
Max
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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