Hi,
My requirement is to write a VBA code in Excel 2010 to merge the multiple excel files into one master excel file. I have written the below code-
**************************
Sub Combine_Multiple_Excel_Files()
Set wb = ThisWorkbook
InitialFoldr$ = ThisWorkbook.Path & "\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select the files folder"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
Workbooks.Open (xDirect$ & xFname$), UpdateLinks:=False
Application.Workbooks(xFname$).Activate
Set srcwb = Workbooks(xFname$)
For Each sh In srcwb.Sheets
LR1 = wb.Sheets(1).Cells(Rows.Count, 3).End(xlUp).Row
LR2 = sh.Cells(Rows.Count, 1).End(xlUp).Row
wb.Sheets(1).Range("A" & LR1 & ":A" & (LR1 + LR2 - 4)).Value = (xDirect$ & xFname$)
wb.Sheets(1).Range("B" & LR1 & ":B" & (LR1 + LR2 - 4)).Value = sh.Name
sh.Range("A4:R" & LR2).Copy Destination:=wb.Sheets(1).Range("C" & LR1)
Next
Application.CutCopyMode = False 'Clear Clipboard
srcwb.Close savechanges:=False
xFname$ = Dir
Loop
End If
End With
Application.GoTo reference:=wb.Sheets(1).[A1]
End Sub
**************************
However, when i run this macro, i receive the following error-
**************************
excel macro run time error 1004
Application defined or object defined error
**************************
When i press Debug, i see that the error is coming at-
**************************
wb.Sheets(1).Range("A" & LR1 & ":A" & (LR1 + LR2 - 4)).Value = (xDirect$ & xFname$)
**************************
I am new to VBA programming. So, Can anyone please help me here? This is urgent.
Thanks,
Karan
Bookmarks