Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Friendly and Informative Error Handling.
Written by Allen Wyatt (last updated April 24, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Other people use my workbooks and I have a simple technique that I use when writing error handlers that makes it easier to cope with problems if macros fail. Every procedure that is at all risky contains a local string variable, sOp, whose value is set during the macro code as follows:
Dim sOp As String ... [your code] ... sOp = "opening target file" ... [your code] ... sOp = "counting lines already filled" ... [your code] ... sOp = "copying source data table" ... [your code] ... sOp = "saving and closing" ... [your code]
These statements are nothing more than one might put in as remarks, but they have the advantage that when an error occurs, the user can be informed what was going on at the time. At its simplest the error handler just needs to contain a single statement like this:
MsgBox "Procedure MyMacro failed while " + vbCrLf + sOp
The value of sOp can also be used to determine the next action (resume, exit, etc). Using this technique in your own macros can make them easier to debug and more friendly for users.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9577) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Friendly and Informative Error Handling.
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!
If you need to limit the cells that are accessible by the user of a worksheet, VBA can come to the rescue. This doesn't ...
Discover MoreWhen developing a macro, it is often necessary to step through the various code lines so you can see what is happening on ...
Discover MoreWant to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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