VBA CHR Function

VBA CHR Function in Excel

VBA CHR function in Excel 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.

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’

Let us see 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’

Let us see another 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

ASCII Table Chart

Please click on below link to see ASCII Table Chart.

ASCII Table Chart

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

2 thoughts on “VBA CHR Function in Excel”

  1. Heya i’m for the first time here. I came across this board and I find It
    truly helpful & it helped me out much. I hope to provide one thing again and aid
    others such as you aided me.

Leave a Reply