3.2. Type Conversion

VBA provides two sets of built-in conversion functions. The first set, which includes Int and Str, is from the early versions of VB and is simply left in for backwards compatibility. The functions of the second set all start with the letter "C" and are the more recent conversion functions. Microsoft recommends that you use this latter set of functions, since they are locale-aware; that is, they take account of international date, time, and number settings on the host system.

The syntax for each of the latter conversion functions is basically the same. For example:

CBool(variablename)

where variablename is either the name of a variable, a constant, or an expression (like x-y) that evaluates to a particular data type. Regardless of the particular function you use, the data type being converted is immaterial; what matters is the data type to which you want to convert a particular value.

The conversion functions supported by VBA are:

CBool

Converts variablename to a Boolean data type. variablename can contain any numeric data type or any string capable of being converted into a number. If variablename is or "0", CBool returns False; otherwise, it returns True (–1).

CByte

Converts variablename to a Byte data type. variablename can contain any numeric data or string data capable of conversion into a number that is greater than or equal to and less than or equal to 255. If variablename is out of range, VBA displays an Overflow error message. If variablename is ...

Get VB & VBA in a Nutshell: The Language 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.