CByte

Coerces an expression to a Byte.

Syntax: CByte( expression )

expression
required Any valid string or numeric expression in the range 0 to 255.

The return type is Byte. Fractions are rounded — when the fractional part is exactly 0.5, CByte rounds to the nearest even number.

Common Errors

Error When
6 (&H6), Overflow expression rounds to a number less than 0 or greater than 255
13 (&HD), Type mismatch expression is a string that does not hold a number (a zero-length string included)
94 (&H5E), Invalid use of Null expression is Null

Example

This example uses the CByte function to convert an expression to a Byte.

Dim MyDouble, MyByte
MyDouble = 125.5678          ' MyDouble is a Double.
MyByte = CByte(MyDouble)     ' MyByte contains 126.

See Also