Hello All!
I'm attempting to copy several workbooks in a folder into one workbook.
The code works when the spreadsheet is saved as a .xlsm. However, when I save the exact same code as Excel Add-ins, I continually get the following error:
Run-time error '1004'": Copy method of Worksheet class failed
Here is the code I'm using:
Sub MergeWorkbooks()
Dim FolderPath As String
Dim File As String
FolderPath = "path location"
File = Dir(FolderPath)
Do While File <> ""
Workbooks.Open FolderPath & File
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Replace(File, ".xlsx", "")
Workbooks(File).Close
File = Dir()
Loop
MsgBox "All workbooks have been added! Please rename all tables."
End Sub
The code works until it has to copy the second workbook in the folder.
The error happens here:
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
I have no idea why this isn't working as an Excel Add-in.
Any help would be greatly appreciated. Thank you!
I'm attempting to copy several workbooks in a folder into one workbook.
The code works when the spreadsheet is saved as a .xlsm. However, when I save the exact same code as Excel Add-ins, I continually get the following error:
Run-time error '1004'": Copy method of Worksheet class failed
Here is the code I'm using:
Sub MergeWorkbooks()
Dim FolderPath As String
Dim File As String
FolderPath = "path location"
File = Dir(FolderPath)
Do While File <> ""
Workbooks.Open FolderPath & File
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Replace(File, ".xlsx", "")
Workbooks(File).Close
File = Dir()
Loop
MsgBox "All workbooks have been added! Please rename all tables."
End Sub
The code works until it has to copy the second workbook in the folder.
The error happens here:
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
I have no idea why this isn't working as an Excel Add-in.
Any help would be greatly appreciated. Thank you!