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.
Syntax
CByte(Expression)
Arguments
- Expression: The numeric value which you want to convert to a byte.
Example
To practically understand how to use the 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 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.
Notes
- If the value specified is a value other than a number or a number that 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.
More Tutorials
⇠ Back to VBA Function List | What is VBA in Excel
Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes