I try to loop all csv files in folder and add them to current workbook but hit sheet not defined error. After I declare sheet, it hits run time error 1004. Method of open of object Workbooks failed. Anyone can help me on this error ?
VBA Code:
Sub AddCSV()
Dim wb As Workbook
Dim FSO As Object, Folder As Object, file As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Users\My\Downloads\")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
For Each file In Folder.Files
Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
For Each sheet In wb.Sheets
sheet.Copy After:=ThisWorkbook.Sheets(1)
Next
wb.Close False
Next
End Sub