VBA Data Types: Byte



To declare a variable that would hold natural numbers that range from 0 to 255, use the Byte data type. Here is an example:

Sub Exercise()
    Dim StudentAge As Byte
End Sub

After declaring the variable, you can assign it a small positive number. Here is an example:

Sub Exercise()
    Dim Value As Byte
    Value = 246
End Sub

You can also use the number in hexadecimal format as long as the number is less than 255.

If you give either a negative value or a value higher to 255, when you attempt to access it, you would receive an error:


To convert a value to a small number, you can use CByte(). The formula to use would be:

Number = CByte(Value to Convert to Byte)

When using CByte(), enter the value to convert in the parentheses.


Home Copyright © 2009-2010 FunctionX, Inc.