Excel / VBA Chr function

Chr is a Text function in Excel that we may use in the VBA.

What is Chr function?

Chr is a Text function in Excel that we may use in the VBA.

However, like many other Excel functions, you do not need to use it as WorksheetFunction object.

The Chr returns the string that contains the character associated with the specified character code (ASCII code).



An example of using Chr function

We will pass a few ASCII values to Chr() function to get the associated character.

The results are displayed in the message box:



Using 1 to 127 ASCII values and writing results in an Excel sheet by Chr

In this example, we will use a For loop and run the loop from 0 to 127 numbers.

In each iteration, the number is passed to Chr function and it returns the associated ASCII value for that.

The number is written in an Excel sheet’s cell by using a Range function.

See the code and output:

Output sample:


New line example by Chr(10)

This example shows the usage of Chr(10) which represents the “Line feed (LF)” character.

We used a for loop that will execute ten times. In each iteration, we will use Chr(10) to add a new line.

VBA Code:



Adding double quotes in a string example

Strings in VBA are enclosed in double quotes.

So, if you want to include double quotes within a string then you may use Chr(34) character (one of the ways).

The following example concatenates a word in double quotes and displays it in the message box: