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.
Thank you in advance,
Max
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