Dealing with Run-Time Errors

In many cases, run-time errors are caused by factors outside your control. For example, suppose you write the following macro to format the numbers in a selected range using the Indian system of lakhs and crores. (The crore is a unit equal to ten million in the Indian numbering system, while the lakh is equal to 100,000.)

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 ...

Get Microsoft® Excel® 2010 Inside Out now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.