The VBA Chr function returns the character corresponding to a supplied character code between 0 and 255.
The syntax of the function is:
Where the CharCode argument is the character code for which you require the corresponding character.
' 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 )char3 = Chr( 33 )
' char2 is now equal to "A"
' char3 is now equal to "!"
After running the above VBA code, the variables char1, char2 and char3 are equal to "a", "A" and "!" respectively.
If you supply an invalid number to the Chr function (i.e. if CharCode < 0 or CharCode > 255), you will get the error: