I prepared a very fast method of data moving (using arrays and working in memory), avoiding Copy and Paste.
Copy this new declarations at your declarations area:
Dim sh As Worksheet, arrCopy As Variant, lastR As Long
Copy this code line before the loop (For i = 1 To ...):
Set sh = mainWorkbook.Sheets(mainWorkbook.Worksheets.count) 'You can use here your sheet where the data will be collected. I used the last sheet for easy testing reason
Replace (in the loop For Each ...) the existing code (tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)) with the next one:
lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row
arrCopy = tempWorkSheet.Range(tempWorkSheet.Range("A" & IIf(lastR = 1, 1, 2)), _
tempWorkSheet.Range("A1").SpecialCells(xlLastCell)).Value
sh.Range("A" & lastR + IIf(lastR = 1, 0, 1)).Resize(UBound(arrCopy, 1), _
UBound(arrCopy, 2)).Value = arrCopy
My solution will copy all sheet content (headers included) in case of empty sheet to collect data and after that, data range starting from the second row.
Your full code as it should be in order to work (untested):
Sub mergeFiles()
'Define variables:
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As FileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim sh As Worksheet, arrCopy As Variant, lastR As Long
Dim tempWorkSheet As Worksheet, lastRtemp As Long
Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks
tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
'You can use here your sheet where the data will be collected. I used the last sheet for easy testing reason
Set sh = mainWorkbook.Sheets(mainWorkbook.Worksheets.count)
'Loop through all selected workbooks
For i = 1 To tempFileDialog.SelectedItems.count
'Open each workbook
Workbooks.Open tempFileDialog.SelectedItems(i)
Set sourceWorkbook = ActiveWorkbook
'Copy each worksheet to the end of the main workbook
Set tempWorkSheet = sourceWorkbook.Worksheets(1)
lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row
lastRtemp = tempWorkSheet.Range("A" & tempWorkSheet.Rows.count).End(xlUp).row
If lastRtemp < 2 Then
MsgBox "The workbook " & tempWorkSheet.Name & " contains less the two rows..."
Else
arrCopy = tempWorkSheet.Range(tempWorkSheet.Range("A" & IIf(lastR = 1, 1, 2)), _
tempWorkSheet.Range("A1").SpecialCells(xlLastCell)).Value
sh.Range("A" & lastR + IIf(lastR = 1, 0, 1)).Resize(UBound(arrCopy, 1), _
UBound(arrCopy, 2)).Value = arrCopy
End If
'Close the source workbook
sourceWorkbook.Close
Next i
End Sub
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)... You need to take the content of the page and drop it. each time, after the last row with data. I avoided "copying" because it would not be the best method... So, would you like to copy the sheet content starting from its second row up to its last row containing data? All its columns are filled up to the same row? If not, which of them to be consider a reference (the longest)? - FaneDuru