Specifies what action to take if an error is encountered while a macro is running. Use ERROR to control whether Microsoft Excel error messages are displayed, or to run your own macro when an error is encountered.
Syntax
ERROR(enable_logical, macro_ref)
Enable_logical is a logical value or number that selects or clears error-checking.
If enable_logical is FALSE or 0, all error-checking is cleared. If error-checking is cleared and an error is encountered while a macro is running, Microsoft Excel ignores it and continues. Error-checking is selected again by an ERROR(TRUE) statement, or when the macro stops running.
If enable_logical is TRUE or 1, you can either select normal error-checking (by omitting the other argument) or specify a macro to run when an error is encountered by using the macro_ref argument. When normal error-checking is active, the Macro Error dialog box is displayed when an error is encountered. You can halt the macro, start single-stepping through the macro, continue running the macro normally, or go to the macro cell where the error occurred.
If enable_logical is 2 and macro_ref is omitted, error-checking is normal except that if the user clicks the Cancel button in an alert message, ERROR returns FALSE and the macro is not interrupted.
If enable_logical is 2 and macro_ref is given, the macro goes to that macro_ref when an error is encountered. If the user clicks the Cancel button in an alert message, FALSE is returned and the macro is not interrupted.
Macro_ref specifies a macro to run if enable_logical is TRUE, 1, or 2 and an error is encountered. It can be either the name of the macro or a cell reference. If enable_logical is FALSE or 0, macro_ref is ignored.
Important Both ERROR(FALSE) and ERROR(TRUE, macro_ref ) keep Microsoft Excel from displaying any messages at all, including the message asking whether to save changes when you close an unsaved workbook. If you want alert messages but not error messages to be displayed, use ERROR(2, macro_ref ).
Remarks
You can use GET.WORKSPACE to determine whether error-checking is on or off.
Examples
ERROR(FALSE) clears error-checking.
ERROR(TRUE, Recover) selects error-checking and runs the macro named Recover when an error is encountered.
The following macro runs the macro ForceMenus if an error occurs in the current macro:
=ERROR(TRUE, ForceMenus)
Related Functions
CANCEL.KEY Disables macro interruption
LAST.ERROR Returns the reference of the cell where the last error occurred
ON.KEY Runs a macro when a specified key is pressed
Return to index