Written by Allen Wyatt (last updated August 19, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Don notes that he and his colleagues have been experiencing Excel crashing when running various macros. To fix the situation, all they have to do is enter the Visual Basic Editor, save, and the macro works correctly again. Don is wondering if anyone might know why this is happening.
The only way to determine what is causing a macro to crash, in this manner, is to test and debug. (Sorry; there is no way around it.) The first step is to determine which "various macros" are experiencing the problem. Once this is determined, you'll want to figure out what the macros have in common. Do they access the same workbooks? Do they utilize a particular worksheet? How about library references? What about common functions used by the macros?
The list of things to check could go on and on, but you get the idea—look for commonalities among the problem macros. Once you determine this, then it will help you determine where the problem is likely to be rooted.
The next thing to do is to test the macros under various conditions. How you exactly do this will depend on the work done by the macros. For instance, if the macros all use the InputBox function, what happens with various information entered? What happens if no information is entered? What happens if different buttons are clicked in the InputBox? What happens if totally stupid information ("Well, nobody would ever enter something like that!) is entered?
Closely related to this testing is figuring out what you expect to be returned by the VBA (and worksheet) functions you may use in your macros. For instance, your macro may need to determine the name of the current workbook. This is done (obviously) with the .Name property, but under some circumstances, the name may not be what you would expect—like when the workbook has yet to be saved. You'll need to think through what you expect from the functions and then question whether your expectations will, under all circumstances, be met.
Finally, you should strongly consider adding some error checking code to your macros. This code could not only gracefully recover from an error, but it could be written to display an error code or write the error code to a text file. This would help identify the problem causing the crash and, thereby, determine how to recover.
If all of this sounds like a lot of work, it can be. Going through the analyzing, testing, and debugging can help make your macros stronger, however.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13907) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...
Discover MoreWant to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can ...
Discover MoreYou can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-08-21 05:24:04
DaveS
Remember, when it comes to resolving seemingly baffling crashes, the internet is your friend. When faced with a seemingly incomprehensible problem with Excel (or Access) VBA, there's usually someone out there who has had a similar experience and figured out a solution.
2023-08-19 15:20:58
J. Woolley
The Tip mentions debugging. For more on that subject,
see https://excelribbon.tips.net/T007732_Debugging_a_Macro.html
and https://www.myonlinetraininghub.com/debugging-vba-code
The Tip also mentions the .Name property. In particular, a macro that references ActiveWorkbook.Path, ActiveWorkbook.Name, or ActiveWorkbook.FullName might not work as expected if the active workbook is new and unsaved. In this case ActiveWorkbook.Path is blank, which is one way to test the workbook's status.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments