Microsoft Office Tutorials and References
In Depth Information
Error-Handling Techniques
Figure 9-7: You can create a message box to display the error code and description.
Error-handling examples
The first example demonstrates an error that you can safely ignore. The SpecialCells method
selects cells that meet a certain criterion.
The SpecialCells method is equivalent to choosing the Home➜Editing➜Find &
Select➜Go To Special command. The Go To Special dialog box provides you with a
number of choices. For example, you can select cells that contain a numeric constant
(nonformula).
In the example that follows, which doesn’t use any error handling, the SpecialCells method
selects all the cells in the current range selection that contain a formula that returns a number. If
no cells in the selection qualify, VBA displays the error message shown in Figure 9-8.
Figure 9-8: The SpecialCells method generates this error if no cells are found.
Sub SelectFormulas()
Selection.SpecialCells(xlFormulas, xlNumbers).Select
‘ ...[more code goes here]
End Sub
 
Search JabSto ::




Custom Search