The Is Functions

VBA has several Is functions that return Boolean values indicating whether or not a certain condition holds. We have already discussed the IsMissing function in connection with optional arguments. Here are some additional Is functions.

7.4.1.1 The IsDate function

This function indicates whether an expression can be converted to a date. It also applies to a cell, in which case it evaluates the contents of the cell. If the contents represent a valid date, the function returns True. For instance, the code:

IsDate(Range("F3"))

will return True if the contents of cell F3 represent a date.

7.4.1.2 The IsEmpty function

This function indicates whether a variable has been initialized or whether a worksheet cell is empty. For example, the code:

If IsEmpty(Range("A1")) Then . . . tests whether or not cell A1 is empty.

7.4.1.3 The IsNull function

This function is used to test whether a variable is Null (that is, contains no data). Note that code such as:

If var = Null Then will always return False because most expressions that involve Null automatically return False. The proper way to determine if the variable var is Null is to write:

If IsNull(var) Then

7.4.1.4 The IsNumeric function

This function indicates whether an expression can be evaluated as a number or whether a cell contains a value that can be evaluated as a number. For instance, if cell A1 contains the data 123 (even if this cell is formatted as text), then the condition in:

If IsNumeric(Range("A1")) Then will evaluate to True. On the other hand, if the cell contains the data 123 Main Street, then the condition will evaluate to False.

0 0

Post a comment