Ad space (banner)
Lesson 15 / 20

Error handling (On Error)

This lesson covers On Error, which deals with errors at run time.

Writing On Error GoTo LabelName jumps to the place you specify when an error occurs. Err.Description tells you what the error was.

The sample code catches the error raised by dividing by zero with On Error GoTo ErrorHandler and displays the error message.

A common early stumble is not appreciating how risky a macro without error handling is. Without it, an unexpected error stops the macro dead and leaves the user staring at an impenetrable error screen.

In professional work, macros used in the business always build in error handling, ready for the unexpected — a missing file, a sheet that does not exist.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VBA directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)