How to Use Excel VBA Try Catch (3 Suitable Examples)

Most computer programming languages provide the Try…Catch…End syntax. Make an effort to avoid making errors, misfits, and mistakes. Nevertheless, if an issue occurs, The code tries to run and it jumps forward to the catch line so that it may be reported and conveyed to the user. Try…Catch…End is not used in VBA. Instead of the Try statement, there are some other statements used in Excel VBA. In this article, we will construe the duplicate use of the Excel VBA try catch statement.

an overview image of Excel VBA try catch

As you can see in the above image, we neglect the error by declaring the On Error Resume Next statement which is the replicate use of the Try Catch statement.


Download Practice Workbook

To practice, please download the Excel Workbook file from the link below.


What Is Excel VBA Try Catch?

To avoid errors, misleadings, mismatches, and misfits, the Catch statement tries to run the code smoothly and jumps to the catch lines. However, there is no explicit “try-catch” statement like in other computer programming languages. Instead of utilizing the Try…Catch…End syntax, we can replicate it by employing the On Error Resume Next or On Error GoTo statement in Excel VBA.

VBA Try-Catch Error:

Displaying Run-time error

As you can see in the above image, there is an overflow in the marked line that leads to a Run-Time error ’11’. Because of that error, the code is unable to display the result of rest two.

Note: Try…Catch…End syntax is available in Visual Basic(VB) and other computer programming languages. On the contrary, it is absent in VBA.

Excel VBA Try Catch: 3 Suitabe Examples

Unlike other computer programming languages, VBA doesn’t have a Catch statement. Instead, there are 3 alternatives users may use.

  • On Error Resume Next statement is to get rid of errors.
  • On Error GoTo 0 statement to find out errors.
  • On Error GoTo [Label] statement to handle compile errors.

In this article, we will illustrate the use of these statements concisely to handle errors.

For your convenience, to run VBA code you need to open code window in Excel.


1. On Error Resume Next

using of On Error Resume Next statement

As you can see in the above image, there is an overflow in the highlighted line that leads to an error. But when we write On Error Resume Next before the formula, the code skips the line of error and continues accordingly. In the Immediate window, we get 2 outputs neglecting the error value.


2. On Error GoTo 0

using On Error GoTo 0 statement

While writing programming languages we are often required to deal with various types of error. In VBA, There is a by-default On Error GoTo 0 statement to figure out the error code and error name. As you observe in the above image, we obtain 2 results neglecting the value of q; However, when we write On Error GoTo 0, the code stops executing and shows the error in a pop-up message box.


3. On Error GoTo [Label]

using On Error GoTo [label] statement

Implementation of the On Error GoTo [Label] statement is very important while dealing with VBA errors. The code will jump to its label destination if it figures out any error in the code. Here, the code jumps to the label(Err:) once it finds out an error(0/0). Therefore, we obtain 3 results whereas we are supposed to get 7 results in the Immediate window.


Frequently Asked Questions(FAQs)

  • Does VBA have a try-catch?

Generally, the difference from other programming languages is, Catch statement doesn’t exist in VBA. However, you can simulate using the On Error GoTo [Label] or On Error Resume Next statement.

  • How do you catch an error in VBA?

Using VBA’s by-default setting On Error GoTo 0, VBA stops executing the code and displays the related error in a pop-up message box.

  • What is the try-catch equivalent in VBA?

On Error GoTo [Label] or On Error Resume Next statement are equivalent to the Try Catch statement in other programming languages in VBA.


📝 Takeaways from This Article

  • Try and Catch issues in programming languages.
  • Alternatives of Catch statement in VBA.
  • On Error Resume Next statement to skip errors.
  • On Error GoTo 0 to display the error message.
  • On Error GoTo [Label] to get rid of errors.

Conclusion

We briefly expound on 3 different alternatives for implementing the Excel VBA try catch statement. Among these, one may help you understand the actual error with the pop-up message, and one will let you set a label for continue from there while errors. Hope you enjoyed your learning and will be able to use the try-catch statement while working with the Excel VBA code. Any suggestions including queries are appreciated. Don’t hesitate to leave your thoughts in the comment section.

Get FREE Advanced Excel Exercises with Solutions!
MD Tanvir Rahman
MD Tanvir Rahman

MD Tanvir Rahman, BUET graduate in Naval Architecture and Marine Engineering, dedicated over a year to the ExcelDemy project. He is an Excel and VBA Content Developer. Having authored 50+ insightful articles, he actively updates and improves over 80 articles, reflecting his commitment to accuracy and currency, managing day-to-day operations, and analyzing and developing Excel and VBA tutorials. His broad interests encompass Data Analysis, Advanced Excel, VBA Macro, Excel Templates, Excel Automation, and Excel Power Query, showcasing a... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo