A while back I built a macro that looks at a list of names, and from that list of names it creates workbooks based on each name of the list. The macro adds some content for each new workbook created, and then saves and closes the new workbook before it then goes to the next name on the list and starts the process over again. The problem I've been having is that Excel will randomly crash after any bit of code that closes any of the new workbooks. When I say randomly, I mean that sometimes it might go through 3 names on the list and crash, other times it might go through 10 names on the list and then crash, but it's always after the portion of code that closes the new workbook.
I've tried a handful of different ways to close the workbooks, but all of them create the same problem.
Workbooks("Workbook_Name").Close SaveChanges:=False
(I've tried setting to both true and false)
ActiveWindows.Close
ActiveWorkbook.Close
Application.Windows("Workbook_Name").Close
The reason I know the issue is with the close function is because when I take it out completely, and just let it run while keeping each new workbook open, it doesn't crash. Obviously, this isn't practical because it puts a huge strain on my system as I open and leave open hundreds of workbooks.
I thought originally that the issue might be timing, and that the workbook was trying to close before other bits of code at finished running, but I have added Application.Wait (Now + TimeValue("00:00:00"))
both before and after the close function with varying wait times, and the issue still persists.
Here is an example of the code immediately surrounding the close function:
'Save workbook
ChDir "Directory_Path"
ActiveWorkbook.SaveAs Filename:="File_Path\"Workbook_Name", _
FileFormat:=xlOpenXMLWorkbook
Application.Wait (Now + TimeValue("00:00:08"))
ActiveWorkbook.Close False
Application.Wait (Now + TimeValue("00:00:03"))
'Delete names from list
Windows("LDS_Test.xlsm").Activate
Sheets("names").Select
Rows("3:3").Select
Selection.Delete Shift:=xlUp
Lastly, I know it isn't a corrupt workbook as I have just recently rebuilt this macro from scratch in a new, clean workbook, hoping that a fresh build out would fix the issue.
Any suggestions?