I did not run into an error for this line of code
wbk1.Sheets("Sheet1").Range("$A:$K").Copy wbk3.Sheets("Copy").Range("$A:$K") 'Change the Sheet name and data range accordingly
However, I did have an issue with this line
wbk2.Sheets("Sheet1").Range("$A2:$K").Copy wbk3.Sheets("Copy").Range("A2" & Rows.Count).End(xlUp).Offset(1, 0) 'Change the Sheet name and data range accordingly
Please retry the full code below. Remember to pay attention to the notes in the code
Sub CombineWBks()
Dim strFirstFile, strSecondFile, strThirdFile, dialogTitle As String
Dim wbk1, wbk2, wbk3 As Workbook
Dim fileDialog As fileDialog
'Speeding up the macro
Application.ScreenUpdating = False
EventState = Application.EnableEvents
Application.EnableEvents = False
CalcState = Application.Calculation
Application.Calculation = xlCalculationManual
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
Application.DisplayAlerts = False
dialogTitle = "Navigate to and select required file."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C:\_ExcelForum\CombineWorkbooks\" 'CHANGE THE INITIAL FILE LOCATION
'.InitialFileName = ThisWorkbook.Path & "\" 'Alternative to previous line
.AllowMultiSelect = True
.Filters.Clear
.Title = dialogTitle
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
strFirstFile = .SelectedItems(1)
strSecondFile = .SelectedItems(2)
End With
strThirdFile = "C:\_ExcelForum\CombineWorkbooks\Test.xlsm"
Set wbk1 = Workbooks.Open(Filename:=strFirstFile)
Set wbk2 = Workbooks.Open(Filename:=strSecondFile)
Set wbk3 = Workbooks.Open(strThirdFile)
'COPY PROCESS BEGINS
wbk1.Sheets("Sheet1").Range("$A:$K").Copy wbk3.Sheets("Copy").Range("$A:$K") 'CHANGE THE SHEET NAME & DATA RANGE ACCORDINGLY
wbk1.Close
wbk2.Sheets("Sheet1").Range("A2:K500000").Copy wbk3.Sheets("Copy").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 'CHANGE SHEET NAMES
wbk2.Close
wbk3.Save
wbk3.Close
'Restoring Speeding up the macro
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAuto
ActiveSheet.DisplayPageBreaks = True
End Sub
Bookmarks