Check Results

In many situations, you need to check the result of an operation to tell if it succeeded. The result-checking functions in Visual Basic let you test results before you take actions that might otherwise cause an error. Table 3-16 lists the result-checking functions.

Table 3-16. Visual Basic result-checking functions

Category

Function

Use to

Boolean tests

IsArray

Tell if a variable is an array

 

IsDate

Tell if a variable contains data that can be converted to a Date

 

IsEmpty

Tell if a variable has not yet been initialized

 

IsError

Tell if a variable contains an Error object

 

IsMissing

Tell if a ParamArray argument was omitted

 

IsNull

Tell if a variable contains no valid data

 

IsNumeric

Tell if a variable contains a value that can be converted to a number

 

IsObject

Tell if a variable is a reference to a valid object

Type tests

TypeName

Get the name of the variable’s type as a string

 

TypeOf

Test the type of a variable within an If block

 

VarType

Get the variable’s type as a VbVarType constant

Most of these tests are used with variables that were declared as Variant or Object data types. Those types of variables can contain many different kinds of data, so it is often necessary to test what the variable contains before proceeding in code.

There are several common uses of this in Excel. The first is ActiveSheet.property, which may refer to a Worksheet, Chart, or other object:

 Sub ChangeSheets( ) Select ...

Get Programming Excel with VBA and .NET 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.