The VBA Chr Function

Related Function:


The VBA Chr function returns the character corresponding to a supplied character code between 0 and 255.

The syntax of the function is:

Chr( CharCode )

Where the CharCode argument is the character code for which you require the corresponding character.

VBA Chr Function Examples

' Return the characters for the codes 97, 65 and 33
Dim char1 As String
Dim char2 As String
Dim char3 As String
char1 = Chr( 97 )
' char1 is now equal to "a"
char2 = Chr( 65 )
' char2 is now equal to "A"
char3 = Chr( 33 )
' char3 is now equal to "!"

After running the above VBA code, the variables char1, char2 and char3 are equal to "a", "A" and "!" respectively.

VBA Chr Function Error

If you supply an invalid number to the Chr function (i.e. if CharCode < 0 or CharCode > 255), you will get the error:

Run-time error '5': Invalid procedure call or argument

VBA Run Time Error 5 Message Box