VBA CByte Function

VBA CByte Function

Objective of VBA CByte Function:


VBA CByte Function is categorized as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CByte function converts an expression to a Byte data type. This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CByte Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CByte function, where we can use this VBA CByte Function and it’s real-time examples.

Syntax of VBA CByte Function:

The syntax of the CByte Function in VBA is

CByte(Expression)

The CByte function returns a byte data type value.

Parameters or Arguments:

The CByte function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a byte value. The value is in between 0 and 255.

Where we can apply or use VBA CByte Function?

We can use this CByte Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Convert a Value (100.2) to Byte

Here is a simple example of the VBA CByte function. This below example converts a value (100.2) to Byte.

'Convert a Value(100.2) to Byte
Sub VBA_CByte_Function_Ex1()

    'Variable declaration
    Dim iValue As Integer
    Dim bResult As Byte
    
    iValue = 100.2
        
    bResult = CByte(iValue)
        
    MsgBox "The value(100.2) in byte is : " & bResult, vbInformation, "VBA CByte Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA CByte Function

Example 2: Convert a Value(100.6) to Byte

Find one more example of the VBA CByte function. This below example converts a value (100.6) to Byte.

'Convert a Value(100.6) to Byte
Sub VBA_CByte_Function_Ex2()

    'Variable declaration
    Dim iValue As Integer
    Dim bResult As Byte
    
    iValue = 100.6
        
    bResult = CByte(iValue)
        
    MsgBox "The value(100.6) in byte is : " & bResult, vbInformation, "VBA CByte Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA CByte Function

Example 3: Convert a String to Byte(Type mismatch error)

Here is another example of the VBA CByte function. This below returns an error. Because we can’t convert string value to byte.

'Convert a String to Byte(Type mismatch error)
Sub VBA_CByte_Function_Ex3()

    'Variable declaration
    Dim iValue As String
    Dim bResult As Byte
    
    iValue = "Example"
        
    bResult = CByte(iValue)
        
    MsgBox "The string('Example') in byte is : " & bResult, vbInformation, "VBA CByte Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA CByte Function

Example 4: Convert a Value(300) to Byte(Overflow error)

Another example of the VBA CByte function. This below example returns an error. The value should be in between 0 and 255. It should not be greater than 255.

'Convert a Value to Byte(Overflow error)
Sub VBA_CByte_Function_Ex4()

    'Variable declaration
    Dim iValue As Integer
    Dim bResult As Byte
    
    iValue = 300
        
    bResult = CByte(iValue)
        
    MsgBox "The value(300) in byte is : " & bResult, vbInformation, "VBA CByte Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA CByte Function

Example 5: Convert a Value(-3) to Byte(Overflow error)

Here is a simple example of the VBA CByte function. This below example returns an error. The value should not be negative.

'Convert a Value to Byte(Overflow error)
Sub VBA_CByte_Function_Ex5()

    'Variable declaration
    Dim iValue As Integer
    Dim bResult As Byte
    
    iValue = -3
        
    bResult = CByte(iValue)
        
    MsgBox "The value(-3) in byte is : " & bResult, vbInformation, "VBA CByte Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA CByte Function

Instructions to use Macro Codes

Here are the step by step instructions to use the CByte Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Data Type Conversion Functions List of VBA Functions

Leave a Comment

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