for few years I've a macro somewhere from the internet which opens all files in selected folder and does something with them (changes something in all of them, merges them in one, changes column order etc.).
For some reason, with some files the macro crashes without any error message. One day it crashes, the other day it does not. It crashes on my computer, as well as on there's computers or it doesn't crash on my computer and it crashes on there's computers etc...
It crashes if there are 50 files in the folder or just two small files.
There is only way I can make the macro run till the end is to create a breakpoint on Set wb = Workbooks.Open(Pathname & Filename) and sometimes even on Do Work wb. And then I press just F5 when the macros stops on this breakpoint(s) and it does everything it should until it reaches the breakpoint again.
Public LastLine As LongPublic final_file As StringPublic my_directory As StringSub ProcessFiles()Dim Filename, Pathname As StringDim wb As WorkbookApplication.AskToUpdateLinks = FalseApplication.ScreenUpdating = FalseApplication.DisplayAlerts = FalseApplication.Calculation = xlCalculationManual Application.EnableEvents = Falsefinal_file = ActiveWorkbook.Namemy_directory = InputBox("What's the name of the folder with the files?:", "What's the name of the folder with the files", "New files")Pathname = ActiveWorkbook.Path & "\"& my_directory & "\"Filename = Dir(Pathname & "*.xlsx")Do While Filename <> "" Set wb = Workbooks.Open(Pathname & Filename) DoWork wb ActiveWindow.Close Filename = Dir()Loop LastLine = 0Application.ScreenUpdating = TrueApplication.AskToUpdateLinks = TrueApplication.DisplayAlerts = TrueApplication.TransitionNavigKeys = FalseApplication.Calculation = xlCalculationAutomaticApplication.EnableEvents = TrueEnd SubSub DoWork(wb As Workbook) With wb'Here it does the tricks with each opened fileEnd Sub
Do you have any idea what can be wrong with this?Thank you