Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3808fd7ad4fc33725ab9d285f95e105c to your computer and use it in GitHub Desktop.
Save anonymous/3808fd7ad4fc33725ab9d285f95e105c to your computer and use it in GitHub Desktop.
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