In many cases, run-time errors are caused by factors outside your control. For example, suppose
you have written the following macro to format the numbers in a selected range using
the Indian system of lakhs and crores:
Sub LakhsCrores()
Dim cell as Object
For Each cell In Selection
If Abs(cell.Value) > 10000000 Then
cell.NumberFormat = "#"",""##"",""##"",""###"
ElseIf Abs(cell.Value) > 100000 Then
cell.NumberFormat = "##"",""##"",""###"
End If
Next cell
End Sub
This macro works fine if the person who runs it selects a range containing numbers before
running the macro. But if the user selects something else—a chart embedded on the worksheet,
for example—VBA displays the error message:
"Run-time error'436':
Object doesn't support the property or method".
On Error Goto label
If a run-time error occurs, the On Error GoTo statement transfers execution to the errorhandling
code. In the case of your LakhsCrores routine, the macro complete with error handling
might look like this:
Sub LakhsCrores()
'Catch run-time error caused by inappropriate selection
On Error GoTo ErrorHandler
For Each cell In Selection
If Abs(cell.Value) > 10000000 Then
cell.NumberFormat = "#"",""##"",""##"",""###"
ElseIf Abs(cell.Value) > 100000 Then
cell.NumberFormat = "##"",""##"",""###"
End If
Next cell
'Exit sub statement keeps execution from entering
'error handler if no error occurs
Exit Sub
'Error handler
ErrorHandler:
MsgBox "Please select a worksheet range"
End Sub
Notice that the error handler goes at the end of the program, introduced by the label that
appeared in the On Error statement. The label must be followed by a colon and must appear
on a line by itself. An Exit Sub statement appears before the error handler. This statement terminates
the macro when no run-time error occurs; without it, execution would “fall into” the
error handler regardless of whether an error occurred. Now when the user runs the macro
after selecting a chart object, the user sees a polite message box instead of a rude run-time
error message.
ErrorHandler:
If Err.Number=438 Then
MsgBox "Please select a worksheet range"
Else
Msgbox "Please select a range without error values"
End If
In case the code is susceptible to some other run-time error that you haven’t anticipated, you
might want to make the handler look like this:
ErrorHandler:
If Err.Number = 438 Then
MsgBox "Please select a worksheet range"
ElseIf Err.Number = 13 Then
MsgBox "Please select a range without error values"
Else
MsgBox "Sorry! Unknown error!"
End If
This isn’t particularly elegant, but at least you’ve got all the bases more or less covered.
The foregoing error-handler examples assume that your program should terminate when a
run-time error occurs. The purpose of the error handler is to prevent the jolting VBA message
from showing up—and to provide the user with a simple explanation of what has
gone wrong.
Sub LakhsCrores()
'Tell VBA to ignore all run-time errors
On Error Resume Next
For Each cell In Selection
If Abs(cell.Value) > 10000000 Then
cell.NumberFormat = "#"",""##"",""##"",""###"
ElseIf Abs(cell.Value) > 100000 Then
cell.NumberFormat = "##"",""##"",""###"
End If
Next cell
Exit Sub
With this code, if the user selects a chart and runs the macro, the run-time error is ignored,
the program moves on to the For Each block, and nothing happens—because nothing can
happen. If the user selects a range containing one or more error values, the program skips
over those cells that it can’t format and formats the ones it can. In all cases, neither error message
nor message box appears, and all is well. This solution is ideal for this particular macro.