I spent endless time removing / replacing code modules to figure out which triggers work against and not work.
Removing / replacing code modules in another book
This is by far the least troubling approach and why I implemented it using Code Module Management . In this case, for any workbook not yet open that can be selected in the file dialog box, the Workbook opens with:
Application.EnableEvents = False ....Open "workbook-fullname" ' selected in a file dialog Application.EnableEvents = False
"Code module management" will not work when the target workbook has been manually opened before (and therefore presented for selection as a list of open books) without interfering with the execution of any VBA code (see here so that this can be achieved ) Of course, this is only a problem when Workbook_Open executes the code.
My conclusion: after the macros have been executed (subprocedures, functions, etc.), a copy of the VBA code is in memory. Therefore, any Delete will not be executed until the completion of the procedure that deleted the code module. Therefore, Import examines an existing code module and imports a code module with the same name with a numeric suffix to make its name unique. And this from the course always happens when ...
Removing / replacing code modules in "ThisWorkbook" *)
So far, I have not found any way to achieve this - except that the class module was not declared in any of the other code modules, which I realized by accident !, Therefore, I tried to temporarily comment on all declarations until Delete . Complete success! Delete and Import worked fine. But when I finally tried not to comment on previously commented lines of ad code, Excel always crashed (in both cases .DeleteLines and .InsertLines and .ReplaceLine). Since I did not find a solution for this, I refused the attempt - at least for now.
See Managing a code module for cleaning, deleting, transferring, exporting, importing, and even synchronizing much less cumbersome, reliable, and reliable. Even choosing the wrong βtargetβ workbook to transfer or delete is not a drama due to the undo feature.
*) The difference between ActiveWorkbook and ThisWorkbook is important here. ThisWorkbook is a workbook that runs VBA code, and ActiveWorkbook may be different. Because usually both are the same, not paying attention to the difference, it does not matter.