I wrote a function to import 2 to 12 excel files on one excel. The fact is that one day I have 4 files and the other I can have 6 files. Never more than 12. I did a for loop to import my files, but if I have only 4 files , when the loop looks for the 5th file It doesn't find it and there is a pop-up "error 1004". I'm trying to find a way so that my function will continu to run even if i have this error. I'd like to run a "macro #2" after my loop.
Dim d As Integer
For d = 2 To 13
Worksheets(d).Cells.ClearContents
Next d
Dim i As Integer
For i = 2 To 12
Dim file_path As String
Dim file_agg As Workbook
Dim lastrow As Long
Name = Worksheets(1).Cells(i, 1)
file_path = "C:\Users\admin\Downloads\"
Set file_agg = Workbooks.Open(file_path & Name & ".xlsx", True, True)
lastrow = file_agg.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
file_agg.Sheets(1).Range("A1:Z" & lastrow).Copy ThisWorkbook.Sheets(i).Range("A1:Z" & lastrow)
file_agg.Close SaveChanges:=False
Next i
'macro #2 (exemple)
.................................................
.............................
............................................
........................