O'Reilly logo

Microsoft® Office Excel 2003 Inside Out by Mark Dodge, Craig Stinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dealing with Run-Time Errors

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 shown here:

The macro generates a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required