Home / VBA / VBA Exit Sub Statement
VBA Exit Sub is a statement that you use to exit a sub-procedure or a function. As you know, each line is a macro executes one after another, and when you add the “Exit Sub” VBA, exit the procedure without running the rest of the code that comes after that. It works best with loops and the message box.
Using Exit Sub Statement in VBA
- First, decide on which line you want to add the “Exit Sub”.
- After that, check the structure of the code that will get executed when you run the code.
- Next, enter the “Exit Sub”.
- In the end, it’s better to have comment that describes why you are using the “Exit Sub” statement.
Note: In a VBA function procedure, the statement that you need to use is “Exit Function”.
Use Exit Sub with a Message Box and Input Box
Let’s say you want to get input from the user with an input box and exit the procedure if the user’s reply is not a number (consider the following example).
In the above code, you have ISNUMERIC that checks for the value entered in the input box if it’s a number or not, and if that value is not a number, it uses the Exit Sub statement to end the procedure after showing a message box.
Sub vba_exit_sub_example()
If IsNumeric(InputBox("Enter your age.", "Age")) = False Then
MsgBox "Error! Enter your Age in numbers only."
Exit Sub
Else
MsgBox "Thanks for the input."
End If
End Sub
On Error Exit Sub
One of the best things about the “Exit Sub” you can use it to exit the procedure when an error occurs. Below is the code that divides a number with a zero that returns a “Run-time error ‘11’ “ and stops the execution.
Here you can use the GoTo statement to create an error handler with the “Exit Sub” to exit the procedure (consider the following code).
Sub vba_exit_sub_on_error()
On Error GoTo iError
Range("A1") = 10 / 0
iError:
MsgBox "You can't divide with the zero." & _
"Change the code."
Exit Sub
End Sub
In the above code, you have an error handler, “iError” with a message box and then the “Exit Sub” Statement. When an error occurs during the calculation, the goto statement jumps to the error handler (VBA Error Handling), and it will exit the procedure.
Обработка ошибок в VBA Excel с помощью оператора On Error. Синтаксис выражений с оператором On Error. Пример кода с простым обработчиком ошибок.
On Error – это оператор, который используется для отслеживания ошибок во время исполнения кода VBA. При возникновении ошибки On Error передает информацию о ней в объект Err и включает программу обработки ошибок, начинающуюся с указанной строки.
В первую очередь, обработчик ошибок нужен для пользователей файлов Excel с кодами VBA. Любая ошибка приводит к прекращению выполнения программы, открытию редактора VBA с непонятным для пользователя сообщением или даже к полному зависанию приложения.
Обработчик ошибок позволяет завершить выполнение программы при возникновении ошибки и вывести сообщение пользователю с ее описанием.
Синтаксис выражений с On Error
Включает алгоритм обнаружения ошибок и, в случае возникновения ошибки, передает управление операторам обработчика ошибок с указанной в выражении строки. Stroka – это метка, после которой расположены операторы обработчика ошибок.
Включает алгоритм обнаружения ошибок и, в случае возникновения ошибки, передает управление оператору, следующему за оператором, вызвавшем ошибку.
Отключает любой включенный обработчик ошибок в текущей процедуре.
Простой обработчик ошибок
Шаблон простейшего обработчика ошибок:
Sub Primer() On Error GoTo Stroka ‘Блок операторов процедуры Exit Sub Stroka: MsgBox «Произошла ошибка: « & Err.Description End Sub |
Оператор On Error GoTo
размещается в начале процедуры, метка и обработчик ошибок – в конце процедуры. Название метки можно сменить на другое, в том числе на кириллице.
Оператор Exit Sub
обеспечивает выход из процедуры, если блок операторов выполнен без ошибок. Для вывода описания ошибки используется свойство Description
объекта Err
.
Примеры обработки ошибок
Пример 1
Деление на ноль:
Sub Primer1() On Error GoTo Инструкция Dim a As Double a = 45 / 0 Exit Sub Instr: MsgBox «Произошла ошибка: « & Err.Description End Sub |
Результат выполнения кода VBA Excel с обработчиком ошибок:
Пример 2
Выход за границы диапазона:
Sub Primer2() On Error GoTo Instr Dim myRange As Range Set myRange = Range(«A1:D4»).Offset(—2) Exit Sub Instr: MsgBox «Произошла ошибка: « & Err.Description End Sub |
Результат выполнения кода VBA Excel с оператором On Error GoTo
:
Пример использования выражений On Error Resume Next
и On Error GoTo 0
смотрите в статье: Отбор уникальных значений с помощью Collection.
Содержание
- Оператор On Error (Visual Basic)
- Синтаксис
- Компоненты
- Комментарии
- Свойство Number
- Оператор Throw
- При следующей ошибке возобновление
- При ошибке GoTo 0
- При ошибке goTo -1
- Неисправляемые ошибки
- VBA On Error Exit Sub
- Exit a Sub on Error in VBA
- VBA Coding Made Easy
- VBA Code Examples Add-in
- On Error Statement (Visual Basic)
- Syntax
- Parts
- Remarks
- Number Property
- Throw Statement
- On Error Resume Next
- On Error GoTo 0
- On Error GoTo -1
- Untrapped Errors
Оператор On Error (Visual Basic)
Включает подпрограмму обработки ошибок и указывает расположение подпрограммы в процедуре; также можно использовать для отключения подпрограммы обработки ошибок. Оператор On Error используется в неструктурированной обработке ошибок и может использоваться вместо структурированной обработки исключений. Структурированная обработка исключений встроена в .NET, как правило, более эффективна и поэтому рекомендуется при обработке ошибок среды выполнения в приложении.
Без обработки ошибок или обработки исключений любая возникающая ошибка во время выполнения неустранима: отображается сообщение об ошибке и выполнение останавливается.
Ключевое Error слово также используется в инструкции error, которая поддерживается для обеспечения обратной совместимости.
Синтаксис
Компоненты
Термин | Определение |
---|---|
GoTo Линии | Включает подпрограмму обработки ошибок, которая начинается со строки, указанной в требуемом аргументе строки . Аргумент строки — это любая метка строки или номер строки. При возникновении ошибки во время выполнения управление ветвляется в указанную строку, делая обработчик ошибок активным. Указанная строка должна находиться в той же процедуре, что и On Error инструкция , иначе возникнет ошибка во время компиляции. |
GoTo 0 | Отключает включенный обработчик ошибок в текущей процедуре и сбрасывает его до Nothing . |
GoTo -1 | Отключает включенного исключения в текущей процедуре и сбрасывает его до Nothing . |
Resume Next | Указывает, что при возникновении ошибки во время выполнения управление переходит к инструкции сразу после инструкции, в которой произошла ошибка, и выполнение продолжается с этой точки. Используйте эту форму, а не On Error GoTo при доступе к объектам. |
Комментарии
Мы рекомендуем по возможности использовать структурированную обработку исключений в коде, а не не неструктурированную обработку исключений и инструкцию On Error . Дополнительные сведения см. в разделе Оператор Try. Catch. Finally.
Обработчик ошибок «включен» — это обработчик, который включается оператором On Error . «Активный» обработчик ошибок — это включенный обработчик, который находится в процессе обработки ошибки.
Если ошибка возникает, когда обработчик ошибок активен (между возникновением ошибки и оператором Resume , Exit Sub , Exit Function или Exit Property ), обработчик ошибок текущей процедуры не сможет обработать ошибку. Элемент управления возвращается в вызывающую процедуру.
Если вызывающая процедура имеет включенный обработчик ошибок, она активируется для обработки ошибки. Если обработчик ошибок вызывающей процедуры также активен, управление передается обратно через предыдущие вызывающие процедуры до тех пор, пока не будет найден включенный, но неактивный обработчик ошибок. Если такой обработчик ошибок не найден, ошибка становится неустранимой в точке, в которой она действительно произошла.
Каждый раз, когда обработчик ошибок передает управление вызывающей процедуре, эта процедура становится текущей процедурой. После обработки ошибки обработчиком ошибок в любой процедуре выполнение возобновляется в текущей процедуре в точке, назначенной оператором Resume .
Подпрограмма обработки ошибок не является процедурой Sub или процедурой Function . Это раздел кода, помеченный меткой строки или номером строки.
Свойство Number
Подпрограммы обработки ошибок используют значение в свойстве Number Err объекта для определения причины ошибки. Подпрограмма должна проверять или сохранять соответствующие значения свойств в объекте Err до возникновения любой другой ошибки или перед вызовом процедуры, которая может вызвать ошибку. Значения свойств в объекте Err отражают только самую последнюю ошибку. Сообщение об ошибке, связанное с Err.Number , содержится в Err.Description .
Оператор Throw
Ошибка, возникающая при Err.Raise использовании метода , задает для Exception свойства только что созданный экземпляр Exception класса . Для поддержки создания исключений производных типов исключений Throw в языке поддерживается оператор . Для этого требуется один параметр, который является экземпляром исключения, который должен быть создан. В следующем примере показано, как эти функции можно использовать с существующей поддержкой обработки исключений:
Обратите внимание, что On Error GoTo оператор перехватывает все ошибки, независимо от класса исключения.
При следующей ошибке возобновление
On Error Resume Next приводит к продолжению выполнения с оператором сразу после оператора, вызвавшего ошибку во время выполнения, или оператором сразу после последнего вызова процедуры, содержащей инструкцию On Error Resume Next . Эта инструкция позволяет продолжить выполнение, несмотря на ошибку во время выполнения. Вы можете поместить подпрограмму обработки ошибок в место возникновения ошибки, а не передавать управление в другое расположение в процедуре. Оператор On Error Resume Next становится неактивным при вызове другой процедуры, поэтому следует выполнить инструкцию в каждой On Error Resume Next из вызываемой подпрограммы, если требуется встроенная обработка ошибок в рамках этой процедуры.
Конструкция On Error Resume Next может быть предпочтительнее, чем On Error GoTo при обработке ошибок, создаваемых при доступе к другим объектам. Проверка Err после каждого взаимодействия с объектом устраняет неоднозначность относительно того, к какому объекту был доступ из кода. Вы можете быть уверены, какой объект поместил код ошибки в Err.Number , а также какой объект изначально создал ошибку (объект, указанный в Err.Source ).
При ошибке GoTo 0
On Error GoTo 0 отключает обработку ошибок в текущей процедуре. Строка 0 не указывается в качестве начала кода обработки ошибок, даже если процедура содержит строку с нумеровкой 0. On Error GoTo 0 Без оператора обработчик ошибок автоматически отключается при завершении процедуры.
При ошибке goTo -1
On Error GoTo -1 отключает исключение в текущей процедуре. Строка -1 не указывается в качестве начала кода обработки ошибок, даже если процедура содержит строку с нумеровкой -1. On Error GoTo -1 Без оператора исключение автоматически отключается при завершении процедуры.
Чтобы предотвратить выполнение кода обработки ошибок при отсутствии ошибки, поместите инструкцию Exit Sub , Exit Function или Exit Property непосредственно перед подпрограммой обработки ошибок, как показано в следующем фрагменте:
Здесь код обработки ошибок следует за оператором Exit Sub и предшествует оператору End Sub , чтобы отделить его от потока процедуры. Код обработки ошибок можно разместить в любом месте процедуры.
Неисправляемые ошибки
Необрезанные ошибки в объектах возвращаются управляющее приложение, когда объект выполняется как исполняемый файл. В среде разработки неиспространенные ошибки возвращаются в управляющее приложение, только если заданы правильные параметры. Сведения о том, какие параметры следует задать во время отладки, как их задать и может ли узел создавать классы, см. в документации ведущего приложения.
При создании объекта, который обращается к другим объектам, следует попытаться обработать все необработанные ошибки, которые они передают обратно. Если вы не можете, сопоставьте коды ошибок в Err.Number с одной из собственных ошибок, а затем передайте их обратно вызывающему объекту. Необходимо указать ошибку, добавив код ошибки в константу VbObjectError . Например, если код ошибки равен 1052, назначьте его следующим образом:
Системные ошибки во время вызовов библиотек динамической компоновки Windows (DLL) не вызывают исключений и не могут быть захвачены с помощью перехвата ошибок Visual Basic. При вызове функций DLL следует проверять каждое возвращаемое значение на успешность или сбой (в соответствии со спецификациями API), а в случае сбоя — значение в Err свойстве LastDLLError объекта .
Источник
VBA On Error Exit Sub
In VBA, you can program your code to Exit a Sub whenever an error is encountered. To do this, use On Error GoTo and Exit Sub.
On Error Goto tells VBA that when an error is encountered to “Go To” a specific section of your code. Ex:
From within that section you can then tell VBA to Exit Sub to exit the sub procedure:
Read on, to see these lines of code in action…
Exit a Sub on Error in VBA
In this example, we will generate an error by dividing by zero:
See the full code below. When VBA reads the error, it will “goto” the ErrorHandler section of code and exit:
To specify error handling, you first need to declare On Error GoTo statement. It should be declared at the beginning of a Sub:
After that you need to declare the error handling code. It is usually at the end of the code:
We add the “Exit Sub” above the ErrorHandler because we only want to run the ErrorHandler code if there’s an error.
If you run the Sub, it will come to an error because of division by zero. In that moment the error handling code will be executed. The message box appears and the Sub is exited.
If you want to learn how to end code execution in VBA, click on this link: VBA End
If you want to learn how to exit a Sub or Function, click on this link: VBA Exit Sub or Function
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
VBA Code Examples Add-in
Easily access all of the code examples found on our site.
Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.
Источник
On Error Statement (Visual Basic)
Enables an error-handling routine and specifies the location of the routine within a procedure; can also be used to disable an error-handling routine. The On Error statement is used in unstructured error handling and can be used instead of structured exception handling. Structured exception handling is built into .NET, is generally more efficient, and so is recommended when handling runtime errors in your application.
Without error handling or exception handling, any run-time error that occurs is fatal: an error message is displayed, and execution stops.
The Error keyword is also used in the Error Statement, which is supported for backward compatibility.
Syntax
Parts
Term | Definition |
---|---|
GoTo line | Enables the error-handling routine that starts at the line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to the specified line, making the error handler active. The specified line must be in the same procedure as the On Error statement or a compile-time error will occur. |
GoTo 0 | Disables enabled error handler in the current procedure and resets it to Nothing . |
GoTo -1 | Disables enabled exception in the current procedure and resets it to Nothing . |
Resume Next | Specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred, and execution continues from that point. Use this form rather than On Error GoTo when accessing objects. |
We recommend that you use structured exception handling in your code whenever possible, rather than using unstructured exception handling and the On Error statement. For more information, see Try. Catch. Finally Statement.
An «enabled» error handler is one that is turned on by an On Error statement. An «active» error handler is an enabled handler that is in the process of handling an error.
If an error occurs while an error handler is active (between the occurrence of the error and a Resume , Exit Sub , Exit Function , or Exit Property statement), the current procedure’s error handler cannot handle the error. Control returns to the calling procedure.
If the calling procedure has an enabled error handler, it is activated to handle the error. If the calling procedure’s error handler is also active, control passes back through previous calling procedures until an enabled, but inactive, error handler is found. If no such error handler is found, the error is fatal at the point at which it actually occurred.
Each time the error handler passes control back to a calling procedure, that procedure becomes the current procedure. Once an error is handled by an error handler in any procedure, execution resumes in the current procedure at the point designated by the Resume statement.
An error-handling routine is not a Sub procedure or a Function procedure. It is a section of code marked by a line label or a line number.
Number Property
Error-handling routines rely on the value in the Number property of the Err object to determine the cause of the error. The routine should test or save relevant property values in the Err object before any other error can occur or before a procedure that might cause an error is called. The property values in the Err object reflect only the most recent error. The error message associated with Err.Number is contained in Err.Description .
Throw Statement
An error that is raised with the Err.Raise method sets the Exception property to a newly created instance of the Exception class. In order to support the raising of exceptions of derived exception types, a Throw statement is supported in the language. This takes a single parameter that is the exception instance to be thrown. The following example shows how these features can be used with the existing exception handling support:
Notice that the On Error GoTo statement traps all errors, regardless of the exception class.
On Error Resume Next
On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This statement allows execution to continue despite a run-time error. You can place the error-handling routine where the error would occur rather than transferring control to another location within the procedure. An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine.
The On Error Resume Next construct may be preferable to On Error GoTo when handling errors generated during access to other objects. Checking Err after each interaction with an object removes ambiguity about which object was accessed by the code. You can be sure which object placed the error code in Err.Number , as well as which object originally generated the error (the object specified in Err.Source ).
On Error GoTo 0
On Error GoTo 0 disables error handling in the current procedure. It doesn’t specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0. Without an On Error GoTo 0 statement, an error handler is automatically disabled when a procedure is exited.
On Error GoTo -1
On Error GoTo -1 disables the exception in the current procedure. It does not specify line -1 as the start of the error-handling code, even if the procedure contains a line numbered -1. Without an On Error GoTo -1 statement, an exception is automatically disabled when a procedure is exited.
To prevent error-handling code from running when no error has occurred, place an Exit Sub , Exit Function , or Exit Property statement immediately before the error-handling routine, as in the following fragment:
Here, the error-handling code follows the Exit Sub statement and precedes the End Sub statement to separate it from the procedure flow. You can place error-handling code anywhere in a procedure.
Untrapped Errors
Untrapped errors in objects are returned to the controlling application when the object is running as an executable file. Within the development environment, untrapped errors are returned to the controlling application only if the proper options are set. See your host application’s documentation for a description of which options should be set during debugging, how to set them, and whether the host can create classes.
If you create an object that accesses other objects, you should try to handle any unhandled errors they pass back. If you cannot, map the error codes in Err.Number to one of your own errors and then pass them back to the caller of your object. You should specify your error by adding your error code to the VbObjectError constant. For example, if your error code is 1052, assign it as follows:
System errors during calls to Windows dynamic-link libraries (DLLs) do not raise exceptions and cannot be trapped with Visual Basic error trapping. When calling DLL functions, you should check each return value for success or failure (according to the API specifications), and in the event of a failure, check the value in the Err object’s LastDLLError property.
Источник
Активизирует подпрограмму обработки ошибок и указывает положение подпрограммы в процедуре; используется также для отключения подпрограммы обработки ошибок.
- On Error GoTo строка — Активизирует подпрограмму обработки ошибок, начало которой определяется обязательным аргументом строка, значением которого может быть любая метка строки или номер строки. Если возвращается ошибка выполнения, управление передается на указанную строку и запускается обработчик ошибок. Аргумент строка должен определять строку в той же процедуре, в которой находится инструкция On Error; в противном случае возникает ошибка компиляции.
- On Error Resume Next — Указывает, что возникновение ошибки выполнения приводит к передаче управления на инструкцию, непосредственно следующую за инструкцией, при выполнении которой возникла ошибка. Рекомендуется при доступе к объектам использовать эту форму инструкции, а не On Error GoTo.
- On Error GoTo 0 — Отключает любой активизированный обработчик ошибок в текущей процедуре.
Замечания
Если не выполнена инструкция On Error, то любая ошибка выполнения является фатальной; это означает, что выводится сообщение об ошибке и выполнение программы прекращается.
«Включенным» обработчиком ошибок называют подпрограмму, которая указана в инструкции On Error; «активным» обработчиком ошибок является включенный обработчик ошибок, который обрабатывает текущую ошибку. Если ошибка возникает в самом обработчике ошибок (в промежутке между возникновением ошибки и выполнением инструкции Resume, Exit Sub, Exit Function или Exit Property), то обработчик ошибок, определенный в текущей процедуре, не может обработать ошибку. Управление в этом случае возвращается в вызывающую процедуру; если в вызывающей процедуре включен обработчик ошибок, то обработка ошибки передается ему. Если этот обработчик ошибок является в данный момент активным, т.е. уже обрабатывает ошибку, то управление снова передается назад в вызывающую процедуру и т.д. до тех пор, пока не будет найден включенный, но не активный обработчик ошибок. Если включенный, но неактивный обработчик ошибок найден не будет, ошибка становится фатальной в том месте программы, в котором она впервые возникла. При каждой передаче управления обработчиком ошибок в вызывающую процедуру эта процедура становится текущей. После завершения обработки ошибки обработчиком в любой процедуре возобновляется выполнение текущей процедуры с той ее части, которая указана в инструкции Resume.
Подпрограмма обработки ошибок не может быть процедурой Sub или Function. Эта подпрограмма должна быть частью программы, которая отмечается с помощью метки строки или номера строки.
Для определения причины ошибки в подпрограммах обработки ошибок используют значение свойства Number объекта Err. Необходимо обеспечить в подпрограммах обработки ошибок проверку или сохранение существенных значений свойств объекта Err перед тем, как может возникнуть новая ошибка или перед вызовом процедуры, в которой может возникнуть новая ошибка. Значения свойств объекта Err описывают последнюю ошибку. Текст сообщения об ошибке, соответствующего коду ошибки Err.Number содержится в свойстве Err.Description.
Конструкция On Error Resume Next задает продолжение выполнения с инструкции, непосредственно следующей за инструкцией, которая привела к ошибке выполнения, или с инструкции, непосредственно следующей за вызывающей инструкцией в процедуре, содержащей конструкцию On Error Resume Next. Это позволяет продолжить исполнение программы несмотря на ошибку выполнения. Это позволяет также встроить подпрограмму обработки ошибок в процедуру, а не передавать управление в другую часть процедуры. Конструкция On Error Resume Next становится неактивной при вызове новой процедуры, поэтому для внутренней обработки ошибок необходимо выполнять инструкцию On Error Resume Next в каждой вызываемой процедуре.
При обработке ошибок, возникающих при доступе к другим объектам, рекомендуется использовать конструкцию On Error Resume Next, а не конструкцию On Error GoTo. Проверка объекта Err после каждого взаимодействия с другим объектом позволяет устранить неопределенность в том, при доступе к какому объекту возникла ошибка. Это позволяет всегда точно знать, какой объект поместил значение кода ошибки в свойство Err.Number, а также в каком объекте возникла ошибка (эта информация содержится в свойстве Err.Source).
Конструкция On Error GoTo 0 отключает обработку ошибок в текущей процедуре. Эта конструкция не задает переход на строку 0 для обработки ошибок, даже если в процедуре имеется строка с номером 0. Если инструкция On Error GoTo 0 не выполнялась, то обработчик автоматически отключается при выходе из процедуры.
Для того, чтобы предотвратить выполнение программы обработки ошибок в тех случаях, когда ошибка не возникла, следует помещать соответствующую инструкцию Exit Sub, Exit Function или Exit Property сразу после подпрограммы обработки ошибки, как в следующем примере:
Sub InitializeMatrix(Var1, Var2, Var3, Var4) On Error GoTo ОбработкаОшибок . . . Exit Sub ОбработкаОшибок: . . . Resume Next End Sub
В этом примере программа обработки ошибок помещена между инструкциями Exit Sub и End Sub, что позволяет отделить ее от части программы, соответствующей нормальному выполнению процедуры. Программу обработки ошибок можно разместить в любом месте процедуры.
Ошибки, которые не были перехвачены, возвращаются в управляющее приложение, если объект был запущен как исполняемый файл. В среде разработчика такие ошибки возвращаются в управляющее приложение только при указании соответствующих параметров. За описанием необходимых значений этих параметров и способов их задания следует обращаться к документации главного приложения. Там же следует проверить, позволяет ли главное приложение создавать классы.
Err.Number = vbObjectError + 1052
Системные ошибки при вызовах библиотек динамической компоновки (DLL) не приводят к возникновению исключений и не перехватываются средствами Visual Basic. При вызове функций из библиотек DLL необходимо проверять, успешно ли возвращается каждое значение (согласно спецификациям API), и в случае неудачи проверять значение свойства LastDLLError объекта Err.
Пример
В начале этой программы инструкция On Error GoTo определяет положение подпрограммы обработки ошибок в процедуре. В данном примере попытка удалить открытый файл приводит к возникновению ошибки с кодом 55. Ошибка обрабатывается в подпрограмме, после чего управление возвращается инструкции, которая привела к возникновению ошибки. Инструкция On Error GoTo 0 отключает перехват ошибок. После этого инструкция On Error Resume Next задает отложенный перехват ошибок, что позволяет точно определить, в каком контексте возникла ошибка, генерируемая в следующей инструкции. Следует отметить, что после обработки ошибки вызывается метод Err.Clear для сброса значений свойств объекта Err.
Sub OnErrorStatementDemo() On Error GoTo ErrorHandler ' Включаем программу обработки ' ошибок. Open "TESTFILE" For Output As #1 ' Открываем файл. Kill "TESTFILE" ' Попытка удалить открытый ' файл. On Error Goto 0 ' Отключаем перехват ошибок. On Error Resume Next ' Откладываем перехват ошибок. ObjectRef = GetObject("MyWord.Basic") ' Запускаем несуществующий ' объект, а затем проверяем ' ошибку механизма управления ' программируемыми объектами. If Err.Number = 440 Or Err.Number = 432 Then ' Выводим сообщение для пользователя и очищаем объект Err. Msg = "Ошибка при попытке открыть программируемый объект!" MsgBox Msg, , "Проверка отложенной ошибки" Err.Clear ' Очищаем поля объекта Err. End If Exit Sub ' Выходим из процедуры, чтобы ' не попасть в обработчик. ErrorHandler: ' Обработчик ошибок. Select Case Err.Number ' Определяем код ошибки. Case 55 ' "Ошибка "Файл уже открыт". Close #1 ' Закрываем открытый файл. Case Else ' Здесь размещаются инструкции для обработки других ошибок... End Select Resume ' Возобновляем выполнение ' со строки, вызвавшей ошибку. End Sub
Избегание условий ошибки
Когда возникает ошибка времени выполнения, хороший код должен ее обрабатывать. Лучшей стратегией обработки ошибок является запись кода, который проверяет условия ошибки и просто избегает выполнения кода, который приводит к ошибке выполнения.
Одним из ключевых элементов сокращения ошибок во время выполнения является запись небольших процедур, которые делают одно . Чем меньше процедур процедур приходится терпеть неудачу, тем проще код в целом — отлаживать.
Избежать ошибки времени выполнения 91 — Объект или С заблокированной переменной блока:
Эта ошибка будет повышена, если объект используется до назначения ссылки. Возможно, у вас есть процедура, которая получает параметр объекта:
Private Sub DoSomething(ByVal target As Worksheet)
Debug.Print target.Name
End Sub
Если target
не назначена ссылка, приведенный выше код вызовет ошибку, которую легко избежать, проверяя, содержит ли объект фактическую ссылку на объект:
Private Sub DoSomething(ByVal target As Worksheet)
If target Is Nothing Then Exit Sub
Debug.Print target.Name
End Sub
Если target
назначению не присвоена ссылка, то непризнанная ссылка никогда не используется, и ошибка не возникает.
Этот способ раннего выхода из процедуры, когда один или несколько параметров недопустимы, называется предложением охраны .
Избегайте ошибки времени выполнения 9 — Подкласс вне диапазона:
Эта ошибка возникает при доступе к массиву за пределами его границ.
Private Sub DoSomething(ByVal index As Integer)
Debug.Print ActiveWorkbook.Worksheets(index)
End Sub
Учитывая, что индекс больше, чем количество листов в ActiveWorkbook
, приведенный выше код вызовет ошибку времени выполнения. Простое предложение охраны может избежать этого:
Private Sub DoSomething(ByVal index As Integer)
If index > ActiveWorkbook.Worksheets.Count Or index <= 0 Then Exit Sub
Debug.Print ActiveWorkbook.Worksheets(index)
End Sub
Большинство ошибок времени выполнения можно избежать, тщательно проверив значения, которые мы используем, прежде чем мы их используем, и разветвляемся на другом пути выполнения, соответственно, используя простой оператор If
— в сторожевых предложениях, который не делает предположений и не проверяет параметры процедуры, или даже в тело более крупных процедур.
Оператор Error
Даже с защитными пунктами, один не может реально всегда учитывать все возможные ошибки , которые могут быть подняты в теле процедуры. Оператор On Error GoTo
инструктирует VBA перейти к метке линии и ввести «режим обработки ошибок» всякий раз, когда во время выполнения происходит непредвиденная ошибка. После обработки ошибки, код может возобновить обратно в «нормальное» исполнение с помощью Resume
ключевое слово.
Линейные метки обозначают подпрограммы : потому что подпрограммы исходят из устаревшего кода BASIC и используют GoSub
GoTo
и GoSub
и Return
чтобы вернуться к «основной» процедуре, довольно легко написать жесткий код спагетти, если все не строго структурировано , По этой причине лучше всего:
- процедура имеет одну и только одну подпрограмму обработки ошибок
- подпрограмма обработки ошибок работает только в состоянии ошибки
Это означает, что процедура, которая обрабатывает его ошибки, должна быть структурирована следующим образом:
Private Sub DoSomething()
On Error GoTo CleanFail
'procedure code here
CleanExit:
'cleanup code here
Exit Sub
CleanFail:
'error-handling code here
Resume CleanExit
End Sub
Стратегии обработки ошибок
Иногда вы хотите обрабатывать разные ошибки с помощью разных действий. В этом случае вы будете проверять глобальный объект Err
, который будет содержать информацию об ошибке, которая была поднята, и действовать соответственно:
CleanExit:
Exit Sub
CleanFail:
Select Case Err.Number
Case 9
MsgBox "Specified number doesn't exist. Please try again.", vbExclamation
Resume
Case 91
'woah there, this shouldn't be happening.
Stop 'execution will break here
Resume 'hit F8 to jump to the line that raised the error
Case Else
MsgBox "An unexpected error has occurred:" & vbNewLine & Err.Description, vbCritical
Resume CleanExit
End Select
End Sub
В качестве общего руководства рассмотрите возможность включения обработки ошибок для всей подпрограммы или функции и обработайте все ошибки, которые могут возникнуть в пределах ее области действия. Если вам нужно обрабатывать ошибки только в секции небольшого сечения кода — включить и выключить обработку ошибок на одном уровне:
Private Sub DoSomething(CheckValue as Long)
If CheckValue = 0 Then
On Error GoTo ErrorHandler ' turn error handling on
' code that may result in error
On Error GoTo 0 ' turn error handling off - same level
End If
CleanExit:
Exit Sub
ErrorHandler:
' error handling code here
' do not turn off error handling here
Resume
End Sub
Номера строк
VBA поддерживает номера строк в стиле legacy (например, QBASIC). Скрытое свойство Erl
можно использовать для идентификации номера строки, которая вызвала последнюю ошибку. Если вы не используете номера строк, Erl
только вернет 0.
Sub DoSomething()
10 On Error GoTo 50
20 Debug.Print 42 / 0
30 Exit Sub
40
50 Debug.Print "Error raised on line " & Erl ' returns 20
End Sub
Если вы используете номера строк, но не последовательно, а затем Erl
возвращает номер последней строки перед командой, вызвавшей ошибку.
Sub DoSomething()
10 On Error GoTo 50
Debug.Print 42 / 0
30 Exit Sub
50 Debug.Print "Error raised on line " & Erl 'returns 10
End Sub
Имейте в виду, что Erl
также имеет только Integer
точность и будет бесшумно переполняться. Это означает, что номера строк за пределами целочисленного диапазона дадут неверные результаты:
Sub DoSomething()
99997 On Error GoTo 99999
99998 Debug.Print 42 / 0
99999
Debug.Print Erl 'Prints 34462
End Sub
Номер строки не так актуален, как утверждение, вызвавшее ошибку, и строки нумерации быстро становятся утомительными и не совсем удобны в обслуживании.
Резюме ключевого слова
Подпрограмма обработки ошибок будет либо:
- выполняются до конца процедуры, и в этом случае выполнение возобновляется в процедуре вызова.
- или используйте ключевое слово
Resume
для возобновления выполнения внутри той же процедуры.
Ключевое слово Resume
должно использоваться только в подпрограмме обработки ошибок, потому что если VBA встречает Resume
не находясь в состоянии ошибки, возникает ошибка времени выполнения 20 «Возобновить без ошибок».
Существует несколько способов, по которым подпрограмма обработки ошибок может использовать ключевое слово Resume
:
-
Resume
используется отдельно, выполнение продолжается в инструкции, вызвавшей ошибку . Если ошибка на самом деле не обрабатывается , прежде чем делать это, то та же ошибка будет поднят снова, и выполнение может войти в бесконечный цикл. -
Resume Next
продолжает выполнение инструкции сразу после инструкции, вызвавшей ошибку. Если ошибка на самом деле не обрабатывается , прежде чем делать это, то выполнение разрешается продолжать с потенциально недействительными данными, которые могут привести к логическим ошибкам и неожиданному поведению. -
Resume [line label]
продолжает выполнение на указанной метке строки (или номер строки, если вы используете номера строк в стиле устаревшего стиля). Обычно это позволяет выполнить некоторый код очистки до того, как будет чисто выйти из процедуры, например, чтобы закрыть соединение с базой данных, прежде чем вернуться к вызывающему.
Вкл.
Сам оператор On Error
может использовать ключевое слово Resume
чтобы проинструктировать среду выполнения VBA для эффективного игнорирования всех ошибок .
Если ошибка не выполняется до этого, то выполнение разрешено продолжать с потенциально недействительными данными, что может привести к логическим ошибкам и неожиданному поведению .
Вышеупомянутый акцент не может быть особо подчеркнут. On Error Resume Next эффективно игнорирует все ошибки и выталкивает их под ковер . Программа, которая взрывается с ошибкой во время выполнения с учетом недопустимого ввода, — это более эффективная программа, чем программа, которая работает с неизвестными / непреднамеренными данными — будь то только потому, что ошибка намного легче идентифицируется. On Error Resume Next
можно легко скрыть ошибки .
Оператор On Error
является областью действия процедур — поэтому в данной процедуре обычно должен быть только один , такой оператор On Error
.
Однако иногда не удается избежать ошибки, и переключение на подпрограмму обработки ошибок только на Resume Next
просто не кажется правильным. В этом конкретном случае утверждение с известным до невозможности может быть обернуто между двумя On Error
:
On Error Resume Next
[possibly-failing statement]
Err.Clear 'resets current error
On Error GoTo 0
Команда On Error GoTo 0
сбрасывает обработку ошибок в текущей процедуре, так что любая дополнительная инструкция, вызывающая ошибку времени выполнения , будет необработанной внутри этой процедуры и вместо этого будет переходить в стек вызовов до тех пор, пока она не будет захвачена активным обработчиком ошибок. Если в стеке вызовов нет активного обработчика ошибок, он будет рассматриваться как необработанное исключение.
Public Sub Caller()
On Error GoTo Handler
Callee
Exit Sub
Handler:
Debug.Print "Error " & Err.Number & " in Caller."
End Sub
Public Sub Callee()
On Error GoTo Handler
Err.Raise 1 'This will be handled by the Callee handler.
On Error GoTo 0 'After this statement, errors are passed up the stack.
Err.Raise 2 'This will be handled by the Caller handler.
Exit Sub
Handler:
Debug.Print "Error " & Err.Number & " in Callee."
Resume Next
End Sub
Пользовательские ошибки
Часто при написании специализированного класса вы хотите, чтобы он поднимал свои собственные конкретные ошибки, и вам понадобится чистый способ для кода пользователя / вызова для обработки этих пользовательских ошибок. Оптимальным способом достижения этого является определение специального типа Enum
:
Option Explicit
Public Enum FoobarError
Err_FooWasNotBarred = vbObjectError + 1024
Err_BarNotInitialized
Err_SomethingElseHappened
End Enum
Используя встроенную константу vbObjectError
пользовательские коды ошибок не перекрываются с зарезервированными / существующими кодами ошибок. Необходимо явно указать только первое значение перечисления, поскольку базовое значение каждого члена Enum
1
больше, чем предыдущий элемент, поэтому базовое значение Err_BarNotInitialized
неявно является vbObjectError + 1025
.
Повышение собственных ошибок времени выполнения
Ошибка выполнения может быть повышена с Err.Raise
оператора Err.Raise
, поэтому пользовательская ошибка Err_FooWasNotBarred
может быть повышена следующим образом:
Err.Raise Err_FooWasNotBarred
Метод Err.Raise
также может принимать пользовательские параметры Description
и Source
— по этой причине рекомендуется также определять константы для хранения каждого пользовательского описания ошибки:
Private Const Msg_FooWasNotBarred As String = "The foo was not barred."
Private Const Msg_BarNotInitialized As String = "The bar was not initialized."
А затем создайте выделенный частный метод для повышения каждой ошибки:
Private Sub OnFooWasNotBarredError(ByVal source As String)
Err.Raise Err_FooWasNotBarred, source, Msg_FooWasNotBarred
End Sub
Private Sub OnBarNotInitializedError(ByVal source As String)
Err.Raise Err_BarNotInitialized, source, Msg_BarNotInitialized
End Sub
После этого реализация класса может просто вызвать эти специализированные процедуры для повышения ошибки:
Public Sub DoSomething()
'raises the custom 'BarNotInitialized' error with "DoSomething" as the source:
If Me.Bar Is Nothing Then OnBarNotInitializedError "DoSomething"
'...
End Sub
Клиентский код может обрабатывать Err_BarNotInitialized
как и любую другую ошибку, внутри своей собственной подпрограммы обработки ошибок.
Примечание: наследие Error
ключевое слово также может быть использован вместо Err.Raise
, но это устаревшее / осуждается.
Why would I want to get out of an Error Handler (after handling) with an Exit Sub instead of just letting it go to the End Sub?
I’m sure it’s simple. I just don’t understand. Thanks for any help.
Example:
Public Sub SubA()
On Error Goto ProcError
''# other code
MsgBox FuncA()
ProcExit:
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
AnthonyWJones
186k35 gold badges232 silver badges305 bronze badges
asked Sep 4, 2009 at 3:53
Your ProcExit label is your place where you release all the resources whether an error happened or not. For instance:
Public Sub SubA()
On Error Goto ProcError
Connection.Open
Open File for Writing
SomePreciousResource.GrabIt
ProcExit:
Connection.Close
Connection = Nothing
Close File
SomePreciousResource.Release
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
answered Sep 4, 2009 at 5:08
AngryHackerAngryHacker
58.4k100 gold badges312 silver badges575 bronze badges
3
Typically if you have database connections or other objects declared that, whether used safely or created prior to your exception, will need to be cleaned up (disposed of), then returning your error handling code back to the ProcExit entry point will allow you to do your garbage collection in both cases.
If you drop out of your procedure by falling to Exit Sub, you may risk having a yucky build-up of instantiated objects that are just sitting around in your program’s memory.
answered Sep 4, 2009 at 3:56
Phil.WheelerPhil.Wheeler
16.7k10 gold badges99 silver badges155 bronze badges
3
Avoiding error conditions
When a runtime error occurs, good code should handle it. The best error handling strategy is to write code that checks for error conditions and simply avoids executing code that results in a runtime error.
One key element in reducing runtime errors, is writing small procedures that do one thing. The fewer reasons procedures have to fail, the easier the code as a whole is to debug.
Avoiding runtime error 91 — Object or With block variable not set:
This error will be raised when an object is used before its reference is assigned. One might have a procedure that receives an object parameter:
Private Sub DoSomething(ByVal target As Worksheet)
Debug.Print target.Name
End Sub
If target
isn’t assigned a reference, the above code will raise an error that is easily avoided by checking if the object contains an actual object reference:
Private Sub DoSomething(ByVal target As Worksheet)
If target Is Nothing Then Exit Sub
Debug.Print target.Name
End Sub
If target
isn’t assigned a reference, then the unassigned reference is never used, and no error occurs.
This way of early-exiting a procedure when one or more parameter isn’t valid, is called a guard clause.
Avoiding runtime error 9 — Subscript out of range:
This error is raised when an array is accessed outside of its boundaries.
Private Sub DoSomething(ByVal index As Integer)
Debug.Print ActiveWorkbook.Worksheets(index)
End Sub
Given an index greater than the number of worksheets in the ActiveWorkbook
, the above code will raise a runtime error. A simple guard clause can avoid that:
Private Sub DoSomething(ByVal index As Integer)
If index > ActiveWorkbook.Worksheets.Count Or index <= 0 Then Exit Sub
Debug.Print ActiveWorkbook.Worksheets(index)
End Sub
Most runtime errors can be avoided by carefully verifying the values we’re using before we use them, and branching on another execution path accordingly using a simple If
statement — in guard clauses that makes no assumptions and validates a procedure’s parameters, or even in the body of larger procedures.
On Error statement
Even with guard clauses, one cannot realistically always account for all possible error conditions that could be raised in the body of a procedure. The On Error GoTo
statement instructs VBA to jump to a line label and enter «error handling mode» whenever an unexpected error occurs at runtime. After handling an error, code can resume back into «normal» execution using the Resume
keyword.
Line labels denote subroutines: because subroutines originate from legacy BASIC code and uses GoTo
and GoSub
jumps and Return
statements to jump back to the «main» routine, it’s fairly easy to write hard-to-follow spaghetti code if things aren’t rigorously structured. For this reason, it’s best that:
- a procedure has one and only one error-handling subroutine
- the error-handling subroutine only ever runs in an error state
This means a procedure that handles its errors, should be structured like this:
Private Sub DoSomething()
On Error GoTo CleanFail
'procedure code here
CleanExit:
'cleanup code here
Exit Sub
CleanFail:
'error-handling code here
Resume CleanExit
End Sub
Error Handling Strategies
Sometimes you want to handle different errors with different actions. In that case you will inspect the global Err
object, which will contain information about the error that was raised — and act accordingly:
CleanExit:
Exit Sub
CleanFail:
Select Case Err.Number
Case 9
MsgBox "Specified number doesn't exist. Please try again.", vbExclamation
Resume
Case 91
'woah there, this shouldn't be happening.
Stop 'execution will break here
Resume 'hit F8 to jump to the line that raised the error
Case Else
MsgBox "An unexpected error has occurred:" & vbNewLine & Err.Description, vbCritical
Resume CleanExit
End Select
End Sub
As a general guideline, consider turning on the error handling for entire subroutine or function, and handle all the errors that may occur within its scope. If you need to only handle errors in the small section section of the code — turn error handling on and off a the same level:
Private Sub DoSomething(CheckValue as Long)
If CheckValue = 0 Then
On Error GoTo ErrorHandler ' turn error handling on
' code that may result in error
On Error GoTo 0 ' turn error handling off - same level
End If
CleanExit:
Exit Sub
ErrorHandler:
' error handling code here
' do not turn off error handling here
Resume
End Sub
Line numbers
VBA supports legacy-style (e.g. QBASIC) line numbers. The Erl
hidden property can be used to identify the line number that raised the last error. If you’re not using line numbers, Erl
will only ever return 0.
Sub DoSomething()
10 On Error GoTo 50
20 Debug.Print 42 / 0
30 Exit Sub
40
50 Debug.Print "Error raised on line " & Erl ' returns 20
End Sub
If you are using line numbers, but not consistently, then Erl
will return the last line number before the instruction that raised the error.
Sub DoSomething()
10 On Error GoTo 50
Debug.Print 42 / 0
30 Exit Sub
50 Debug.Print "Error raised on line " & Erl 'returns 10
End Sub
Keep in mind that Erl
also only has Integer
precision, and will silently overflow. This means that line numbers outside of the integer range will give incorrect results:
Sub DoSomething()
99997 On Error GoTo 99999
99998 Debug.Print 42 / 0
99999
Debug.Print Erl 'Prints 34462
End Sub
The line number isn’t quite as relevant as the statement that caused the error, and numbering lines quickly becomes tedious and not quite maintenance-friendly.
Resume keyword
An error-handling subroutine will either:
- run to the end of the procedure, in which case execution resumes in the calling procedure.
- or, use the
Resume
keyword to resume execution inside the same procedure.
The Resume
keyword should only ever be used inside an error handling subroutine, because if VBA encounters Resume
without being in an error state, runtime error 20 «Resume without error» is raised.
There are several ways an error-handling subroutine may use the Resume
keyword:
Resume
used alone, execution continues on the statement that caused the error. If the error isn’t actually handled before doing that, then the same error will be raised again, and execution might enter an infinite loop.Resume Next
continues execution on the statement immediately following the statement that caused the error. If the error isn’t actually handled before doing that, then execution is permitted to continue with potentially invalid data, which may result in logical errors and unexpected behavior.Resume [line label]
continues execution at the specified line label (or line number, if you’re using legacy-style line numbers). This would typically allow executing some cleanup code before cleanly exiting the procedure, such as ensuring a database connection is closed before returning to the caller.
On Error Resume Next
The On Error
statement itself can use the Resume
keyword to instruct the VBA runtime to effectively ignore all errors.
If the error isn’t actually handled before doing that, then execution is permitted to continue with potentially invalid data, which may result in logical errors and unexpected behavior.
The emphasis above cannot be emphasized enough. On Error Resume Next effectively ignores all errors and shoves them under the carpet. A program that blows up with a runtime error given invalid input is a better program than one that keeps running with unknown/unintended data — be it only because the bug is much more easily identifiable. On Error Resume Next
can easily hide bugs.
The On Error
statement is procedure-scoped — that’s why there should normally be only one, single such On Error
statement in a given procedure.
However sometimes an error condition can’t quite be avoided, and jumping to an error-handling subroutine only to Resume Next
just doesn’t feel right. In this specific case, the known-to-possibly-fail statement can be wrapped between two On Error
statements:
On Error Resume Next
[possibly-failing statement]
Err.Clear 'resets current error
On Error GoTo 0
The On Error GoTo 0
instruction resets error handling in the current procedure, such that any further instruction causing a runtime error would be unhandled within that procedure and instead passed up the call stack until it is caught by an active error handler. If there is no active error handler in the call stack, it will be treated as an unhandled exception.
Public Sub Caller()
On Error GoTo Handler
Callee
Exit Sub
Handler:
Debug.Print "Error " & Err.Number & " in Caller."
End Sub
Public Sub Callee()
On Error GoTo Handler
Err.Raise 1 'This will be handled by the Callee handler.
On Error GoTo 0 'After this statement, errors are passed up the stack.
Err.Raise 2 'This will be handled by the Caller handler.
Exit Sub
Handler:
Debug.Print "Error " & Err.Number & " in Callee."
Resume Next
End Sub
Custom Errors
Often when writing a specialized class, you’ll want it to raise its own specific errors, and you’ll want a clean way for user/calling code to handle these custom errors. A neat way to achieve this is by defining a dedicated Enum
type:
Option Explicit
Public Enum FoobarError
Err_FooWasNotBarred = vbObjectError + 1024
Err_BarNotInitialized
Err_SomethingElseHappened
End Enum
Using the vbObjectError
built-in constant ensures the custom error codes don’t overlap with reserved/existing error codes. Only the first enum value needs to be explicitly specified, for the underlying value of each Enum
member is 1
greater than the previous member, so the underlying value of Err_BarNotInitialized
is implicitly vbObjectError + 1025
.
Raising your own runtime errors
A runtime error can be raised using the Err.Raise
statement, so the custom Err_FooWasNotBarred
error can be raised as follows:
Err.Raise Err_FooWasNotBarred
The Err.Raise
method can also take custom Description
and Source
parameters — for this reason it’s a good idea to also define constants to hold each custom error’s description:
Private Const Msg_FooWasNotBarred As String = "The foo was not barred."
Private Const Msg_BarNotInitialized As String = "The bar was not initialized."
And then create a dedicated private method to raise each error:
Private Sub OnFooWasNotBarredError(ByVal source As String)
Err.Raise Err_FooWasNotBarred, source, Msg_FooWasNotBarred
End Sub
Private Sub OnBarNotInitializedError(ByVal source As String)
Err.Raise Err_BarNotInitialized, source, Msg_BarNotInitialized
End Sub
The class’ implementation can then simply call these specialized procedures to raise the error:
Public Sub DoSomething()
'raises the custom 'BarNotInitialized' error with "DoSomething" as the source:
If Me.Bar Is Nothing Then OnBarNotInitializedError "DoSomething"
'...
End Sub
The client code can then handle Err_BarNotInitialized
as it would any other error, inside its own error-handling subroutine.
Note: the legacy Error
keyword can also be used in place of Err.Raise
, but it’s obsolete/deprecated.
While writing Excel Macros we generally put more focus on the coding part and getting the desired result but during this process, we forget an important thing i.e. Error handling. Error handling is an important part of every code and VBA On Error Statement is an easy way for handling unexpected exceptions in Excel Macros.
A well-written macro is one that includes proper exception handling routines to catch and tackle every possible error. Error handling is important because in case of any unexpected exceptions your code doesn’t break. Even if any fatal unexpected error occurs in the code then also you should ensure that the code should terminate gracefully.
Definition of VBA On Error Statement:
On Error statement instructs VBA Compiler, what to do in case any runtime exception is thrown.
Syntax of On Error Statement:
Basically, there are three types of On Error statement:
- On Error Goto 0
- On Error Resume Next
- On Error Goto<label>:
On Error Goto 0
This is also called VBA default exception handling. When On Error Goto 0 is in effect, it is the same as having no error handler in the code. Here we are instructing the program to display the standard runtime message box with ‘Continue’, ‘End’, ‘Debug’, and ‘Help’ buttons.
This message box will give you four options:
a. Continue: This will ignore the exception and continue the code, only if it is possible to do so.
b. End: This will terminate the program.
c. Debug: This option will bring the program control back to the statement from where the exception has occurred. This helps you to debug the code.
d. Help: This button will open Microsoft MSDN help pages for that exception.
On Error Resume Next
It is the second form of the On Error statement. This statement tells the VBA program to ignore the error and resume the execution with the next line of code.
On Error Resume Next statement doesn’t fix the runtime errors but it simply means that program execution will continue from the line following the line that caused the error. However, it is the sole responsibility of the programmer to make sure that any handled error should not have any side effects (like uninitialized variables or null objects) on the program execution.
This can be ensured by using the VBA Err object. Err object in VBA comes into the picture whenever any runtime error occurs. The Err object preserves information about one exception at a time. When an exception occurs, the Err object is updated to include information about that exception.
For instance:
For example, I have a simple macro as follows:
Sub GetErr()
On Error Resume Next
N = 1 / 0 ' Line causing divide by zero exception
For i = 1 To N
'SomeSet of Statements
Next i
End Sub
Now as you can clearly see, that in this macro Line 3 causes an exception. But as we are using the On Error Resume Next statement so this line will be skipped and the control will flow to the next statement. But the next statement is a loop that is dependent on the value of ‘N’, and at this step ‘N’ is uninitialized so this will have a side effect on the whole flow of the program.
Now, have a look at the same program after exception handling:
Sub GetErr()
On Error Resume Next
N = 1 / 0 ' Line causing divide by zero exception
If Err.Number <> 0 Then
N = 2 ' Some minimum value of N if there is some exception in the code.
End If
For i = 1 To N
'SomeSet of Statements
Next i
End Sub
Now, here in this code, we are checking the Err. Number property, if it is not equal to zero that means there is some exception in the code. And hence we have set ‘N’ to its minimum value so that there are no side effects in the code due to uninitialized variables.
On Error Goto<label>:
This is the third form in which VBA On Error statement can be used. This statement tells the VBA to transfer the program control to the line followed by the label, in case any runtime errors are encountered. In such cases, all the statements between the exception line and the label will not be executed.
This method is more suitable for exiting the program gracefully if any fatal error occurs during the execution.
Example:
Below is a self-explanatory example of ‘On Error Goto<label>:’ where I have used the label name as ‘Error_handler’.
Sub GetErr()
On Error GoToError_handler:
N = 1 / 0 ' cause an error
MsgBox "This line will not be executed"
Exit Sub
Error_handler:
MsgBox "exception handler"
End Sub
In this code as soon as the exception occurs at Line 3, the program transfers the control to Line 6.
Notice that here I have used ‘Exit Sub’ just before the ‘Error_handler:’ label, this is done to ensure that the Error handler block of code doesn’t execute if there is no error. If you omit the ‘Exit Sub’ statement then the Error handler code block will always execute even if no exception is encountered.
So, this was all about the On Error statement in Excel VBA.