This code allows user to select the files they want to merge together, I'm getting an error on the marked line when I try to copy the sheets from the files to the destination workbook (xlBook
).
I might have the wrong approach; I've been using a lot of examples from google, with no luck.
Sub complie_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim xlBook, srcBook As Workbook
Dim fileSlct, pdfDialog As fileDialog
Dim xlSheet, srcSheet As Worksheet
Dim xlRow, srcRow, xlColm, srcColm As Long
Dim fileIdx As Integer
Dim hdrRang As Range
Set xlBook = ThisWorkbook.Sheets
On Error GoTo error
Set fileSlct = Application.fileDialog(msoFileDialogFilePicker) 'Allows user to select the files/reports
With fileSlct
.AllowMultiSelect = True 'Allows for multi seletion
.Title = "Select target files:"
.ButtonName = "Open"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xlsb; *.xls; *.xlw"
.Show
End With
If fileSlct.SelectedItems.Count = 0 Then
MsgBox "No file found that match.", vbExclamation
Exit Sub
End If
For fileIdx = 1 To fileSlct.SelectedItems.Count 'Loops through each of the selected items, and copies them to workbook
Set srcBook = Workbooks.Open(fileSlct.SelectedItems(fileIdx))
Set xlSheet = srcBook.ActiveSheet
srcRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
srcColm = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(srcRow, srcColm)).Copy xlBook.Sheets("Sheet2").Cells(1, 1) 'Error here
Next fileIdx
Application.ScreenUpdating = True
error:
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub