Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type.

Syntax

CByte (Expression)

Parameter

Expression (required) - This parameter represents the expression that that you want to convert to a Boolean.

Return

This function returns a byte integer values ranging between 0 and 255 after converting the given expression into byte data type.

Example 1

Sub ByteFunction_Example1()
 Dim byte_val As Byte
 byte_val = CByte(20)
 ' the variable byte_val will return 20
 Cells(1, 1).Value = byte_val
 End Sub 

Output

20

VBA CByte Function

Example 2

Sub ByteFunction_Example2()
 Dim byte_val As Byte
 byte_val = CByte(2.98)
 ' the variable byte_val will return 3
 Cells(1, 1).Value = byte_val
 End Sub 

Output

3

VBA CByte Function