VBA CHR Function

VBA CHR Function

Objective of CHR Function

VBA CHR function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel VBA. This function returns the character of the given ASCII value (or Character Code). The ASCII value should be between 0 and 255. ASCII stands for American Standard Code for Information Interchange. It has one input parameter. The CHR function can be used in only VBA. We can’t use this function as a worksheet function. This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CHR function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the CHR function, where we can use this VBA CHR function and real-time examples.
Table of Contents

Syntax of VBA CHR Function

The syntax of the VBA CHR function is

Chr (CharCode)

The VBA Chr function returns a string for the given character code.

Parameters or Arguments

The CHR function has one input (integer) parameter or argument.
Where
CharCode: It is a required parameter. The CharCode is the character code for which we want to display respective ASCII character.

Where we can apply or use VBA CHR Function?

We can use this VBA CHR function in 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: Display the ASCII character of ‘65’

Here is a simple example of the VBA CHR function. This below example macro takes ‘65’ as input and generates output of specified value ASCII character.

'Display the ASCII character of '65'
Sub VBA_CHR_Function_Ex1()

    Dim sOutput As String, iInput_Val As Integer
    
    iInput_Val = 65
    
    sOutput = Chr(iInput_Val)
    
    MsgBox "The ASCII character of '65' is :" & sOutput, vbInformation, "VBA CHR Function"

End Sub 

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

Example 2: Display the ASCII character of ’97’

Here is another example of the VBA CHR function. This below example macro takes ‘97’ as input and generates output of specified value ASCII character.

'Display the ASCII character of '97'
Sub VBA_CHR_Function_Ex2()

    Dim sOutput As String, iInput_Val As Integer
    
    iInput_Val = 97
    
    sOutput = Chr(iInput_Val)
    
    MsgBox "The ASCII character of '97' is :" & sOutput, vbInformation, "VBA CHR Function"

End Sub 

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

Example 3: Display the ASCII character of ’64’

Here is a simple example of the VBA CHR function. This below example macro takes ‘64’ as input and generates output of specified value ASCII character.

'Display the ASCII character of '64'
Sub VBA_CHR_Function_Ex3()

    Dim sOutput As String, iInput_Val As Integer
    
    iInput_Val = 64
    
    sOutput = Chr(iInput_Val)
    
    MsgBox "The ASCII character of '64' is :" & sOutput, vbInformation, "VBA CHR Function"

End Sub 

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

Example 4: Display the ASCII character of ‘270’

Here is a simple example of the VBA CHR function. This below example macro takes ‘270’ as input and generates an error. Because specified input could be between 0 and 255. Otherwise, it returns an error.
Error Description: Run Time Error – Invalid Procedure Call or Argument

'Display the ASCII character of '270'
Sub VBA_CHR_Function_Ex4()

    Dim sOutput As String, iInput_Val As Integer
    
    iInput_Val = 270
    
    sOutput = Chr(iInput_Val)
    
    MsgBox "The ASCII character of '270' is :" & sOutput, vbInformation, "VBA CHR Function"

End Sub

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

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA CHR 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’.

ASCII Table Chart

Please click on below link to see ASCII Table Chart.
ASCII Table

List of VBA Functions:

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

List of VBA Functions

Leave a Comment

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