1st post, but not 1st time visitor - this forum has been an invaluable help in developping my VBA coding skills over the last 3 years - so a big thank you.
Here's a weird issue I've encountered.
I've created a month end template for my team to use. This template stores a number of Macros, including some activated by keyboard short cuts. Those short cuts activated macros are all run on other reports in CSV format.
I've ensured that the macro would stop running if the active workbook was the main workbook, as it does a lot of formatting and would seriously damage the main workbook.
So this fail safe short code will be run hundreds of time a day..... and occasionally, once or twice a month, excel gets confused. Even though the ActiveWorkbook isn't ThisWorkbook, it acts like it is and aborts the macro.
'Prevents from running the macro on the Month End Allocation Template
Dim w As Workbook
Set w = ActiveWorkbook
If w Is ThisWorkbook Then
Exit Sub
End If
I can't really get my head around it, the code is pretty simple, and has 100% prevented unwanted damages to the main workbook (the main thing really), but occasionally prevents the formatting macro to be run other workbooks.
In those rare instances it happens, the user has to close all excel workbooks, and once reopened, it will work again just fine. :|
Is it just an excel bug? Is there something here I didn't consider and should this code be improved? I love its simplicity, and it has 100% avoided damages to the main workbook, I'm just annoyed that excel seemingly gets confused by what ThisWorkbook is/should be, and that i cannot explain it.
Set w = Workbooks.Open(Path & name)orSet w = Workbooks(Name & extension)the use of theActiveWorkbookshould be avoided as much as possible. - Damian.Selectedcell or range, you will want to avoid relying on the.Activeworkbook &/or worksheet. Relying on these methods leaves room for user error which is no good. There is no need to do this when you can explicitly state what your two books are as noted by @Damian - urdearboy