The VBA CBYTE function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it returns a value into a byte date type (ranging from 0 to 255). In simple words, when you specify an expression as a number, it converts it to an integer and returns it in the result.
- Expression: The numeric value which you want to convert to a byte.
To practically understand how to use VBA CBYTE function, you need to go through the below example where we have written a vba code by using it:
Sub example_CBYTE() Range("B1").Value = CByte(Range("A1")) End Sub
In the above example, we have used the value from the cell A1 where we have a number (8.5694) and then we have used the CBYTE function to convert that value into Byte data type and it has returned 9 in the result.
- If the value specified is a value other than a number or a number which can’t be recognized as a number, VBA will return the run-time 13 error.
- And if the range of the value supplied is out of 0 to 255 VBA returns the run-time 6 error.
- If the value specified is not a floating point number then that will be rounded to the nearest integer.