Using the On Error statement
In this recipe, we will be using the On Error
statement in a procedure.
Instead of trying to anticipate every possible error that can be made or invoked, the On Error
statement handles every and all possible errors.
This statement lets you bypass Excel's error handling – the dialog boxes – and allows you to use your own error-handling code.
Let's see how it's done.
Getting ready
ErrorHandling.xlsm
is still open, with Sheet1 active. Both Excel and the VBA Editor windows are visible next to each other.
How to do it…
Insert a new module and follow these steps:
- Enter the following code:
Sub Test() Dim Num As Variant Dim Msg As String On Error GoTo WrongEntry 'Set up error handling Num = InputBox("Enter a value to divide by"...