VBA Byte Data Type in Excel

Byte Data Type in Excel VBA

Byte Data Type in Excel VBA 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 shouln’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.

Byte Syntax in Excel VBA

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.

Example1: The Byte variable value is 200

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

Example2: The Byte variable value is 400

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

Example3: The Byte variable value is negative

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.

VBA Overflow Error

Convert an Expression to Byte VBA Data Type

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.

VBA CByte Function

Also read about all other data types.

VBA Data Types

Leave a Comment

Your email address will not be published. Required fields are marked *