Created
February 6, 2017 14:59
-
-
Save anonymous/3808fd7ad4fc33725ab9d285f95e105c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub MergeExcelFiles() | |
Dim FilesToOpen | |
Dim x As Integer | |
Dim wb As Workbook | |
Dim nrow As Long | |
On Error GoTo ErrHandler | |
Application.ScreenUpdating = False | |
FilesToOpen = Application.GetOpenFilename _ | |
(FileFilter:="Microsoft Excel Files (*.xlsx), *.xlsx", MultiSelect:=True, Title:="Files to Merge") | |
If TypeName(FilesToOpen) = "Boolean" Then | |
MsgBox "No Files were selected" | |
GoTo ExitHandler | |
End If | |
x = 1 | |
While x <= UBound(FilesToOpen) | |
Set wb = Workbooks.Open(Filename:=FilesToOpen(x)) | |
If x = 1 Then | |
wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(1).Range("A1") | |
Else | |
lr = ThisWorkbook.Sheets(1).UsedRange.Rows.Count | |
wb.Sheets(1).UsedRange.Offset(1).Copy ThisWorkbook.Sheets(1).Range("A" & lr + 1) | |
End If | |
wb.Close False | |
x = x + 1 | |
Wend | |
ExitHandler: | |
Application.ScreenUpdating = True | |
Exit Sub | |
ErrHandler: | |
MsgBox Err.Description | |
Resume ExitHandler | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment