Error Handling in VBA

Source: Internet
Author: User
Tags continue error handling integer try catch valid

In theory, VBA does not provide any error-handling mechanisms, and the Visual Basic language-based scripting language used in Microsoft Office products does not have any error handling at all, and VBA automatically navigates to the wrong line of code when an error occurs or an exception occurs. The user is then prompted for possible reasons for the error. This is a typical scripting language error hint, Lenovo to the JavaScript language, in the browser if there is a script error, the browser will give the message, but this does not affect the normal operation of the entire program, the most is the error after the script is not continue to explain it. Even so, JavaScript provides a better error-handling mechanism, such as common try catch statements and alert prompts, and later supported debugger debugging information. JavaScript, while supporting object-oriented language features, has gradually improved its error handling and debugging methods.

However, visual Basci was not so fortunate that, from the very beginning, Visual Basic did not provide a better error-handling mechanism, although we would always encounter such or such run-time errors in practical applications (such as error-removal files, insufficient disk drive space, Network traffic is abnormal, etc.), but there is no error handling for the Visual Basic process, and the program stops running when the error is generated until the exception is cleared. For a more detailed article that describes the error handling and debugging methods of Visual Basic, readers can refer to this link below.

Http://www.officexy.com/Articles/office/VBABasic/20061026103436501.htm

VBA's language features are similar to those of Visual Basic and should be said to belong to the same family, so the method used to handle program exceptions in Visual Basic can also be used in VBA.

In Visual Basic, common program error handling is to set up or use an error trap to tell the application where the error occurs (or to handle the code to run when the error occurs), by defining the tag in the code to tell the application where the error is to be transferred. This is the same as error handling in the C-series language. The basic steps are as follows:

1. Set up a valid error trap to tell the application where the error occurred and where to continue running. The On Error statement in Visual Basic can make the error trap valid and specify an entry for error handling for the application.

2. Write a specific implementation of the response error at the entrance of the error program, such as continuing to try to execute the previous code, or informing the user of the specific cause of the error so that the user tries to resolve it.

3. Exit error handling.

For more information on how to use Visual Basic error handling and the specific meaning of the On Error statement, the reader can carefully read the link's article above with a very detailed description. I'm here to talk about how to use error handling specifically in VBA in the actual application.

Let's look at a simple example.

Private Sub CommandButton1_Click()
On Error GoTo Err_Handle
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = 10
b = 0
c = a / b '除数为0会导致运行时错误
MsgBox c
End Sub
Err_Handle:
MsgBox Err.Description
End Sub

In the above procedure, we first set an error trap through the On Error statement, which is automatically activated, and the error trap points to the label Error_handle defined in the code. When a procedure is invoked, if an exception occurs, the program automatically runs the code snippet that the label points to, giving the user a hint. The Err object is a system object that contains the description and error number when the error occurred, and based on the simple information provided by the Err object, we may be able to tell the user what happened to the application, and ultimately find out the exact cause of the error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.