VBA CBYTE Function

HomeVBA Functions LIST (Category Wise)How to use the VBA CBYTE Function (Syntax + Example)

What is VBA CBYTE Function

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.

how to use it

To use VBA's CBYTE function you need to understand its syntax and arguments:

Syntax

CByte(Expression)

Arguments

  • Expression: The numeric value which you want to convert to a byte.

Example to use CBYTE Function in VBA

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.

Notes

Below are some important points which you need to take care while using CBYTE function in VBA.

  • 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.

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.