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 Long
Public final_file As String
Public my_directory As String
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Application.AskToUpdateLinks = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
final_file = ActiveWorkbook.Name
my_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 = 0
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.TransitionNavigKeys = False
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
Sub DoWork(wb As Workbook)
With wb
'Here it does the tricks with each opened file
End Sub
Do you have any idea what can be wrong with this? Thank you
'in front of the lines, application.screenupdating, application.displayalerts. This will make excel show any messages there are, and could help you identify the problems at hand.Dim Filename As String, Pathname As StringActiveWindow.Closewithwb.CloseClean Projectfeature. It will rebuild the project, removing any code residue that has built up over the years.