VBA Byte Data Type in Excel explained with syntax and examples. It contains binary data and holds a value from 0 through 255. Byte type is categorized as a numeric data type. The default value of byte is 0. It is used to save space in memory. It is smaller than integer data type. We shouldn’t assign negative number or greater than 255 value to byte variable. If we assign it returns or throws an overflow error. We can add a prefix literal &b or &B. It denotes a binary literal.
Let us see Data Type Byte Syntax in Excel VBA.
Dim VariableName As Byte
Where VariableName represents the name of the variable.
The Byte represents the data type of the variable.
Here is an example on Byte VBA Data Type in Excel VBA when value is 200.
Sub VBAF1_Byte_DataType() 'Variable declaration Dim bByte As Byte 'Assign value to variable bByte = 200 End Sub
Let us see one more example when value is 400.
Sub VBAF1_Byte_DataType() 'Variable declaration Dim bByte As Byte 'Assign value to variable bByte = 400 End Sub
Sub VBAF1_Byte_DataType() 'Variable declaration Dim bByte As Byte 'Assign value to variable bByte = -600 End Sub
The above two macro throws an error. Because the byte variable value is more than 255 in the second example and value is negative in third example. Here is screenshot for your reference.
You can convert an expression to byte VBA data type using VBA CByte function. Click on the following link to learn about CByte function and complete tutorial and examples.
Also read about all other data types.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.