Chr, Chr$ Functions

See Also37EIPK              Example2IETZ6R>Low

Returns a one-character string whose ANSI 5221FB code is the argument.




Chr returns a Variant8PHEAW3; Chr$ returns a String.

The argument charcode is an integer between 0 and 255, inclusive.

Applications for Microsoft Windows use the ANSI character set108ABF.  ANSI character codes in the range 0 to 31, inclusive, are the same as the standard, nonprintable ASCII1KYP8NO codes.  For example, Chr(10) returns a linefeed character.  It can be used to force a new line when message strings are formatted with MsgBox or InputBox.

See Also

Asc FunctionLANASC

Str, Str$ FunctionsLANSTR

Chr, Chr$ Function Example

The example uses the Chr function to create a variable that will insert a newline character as well as a variable containing letters from A through Z.  Each time the line containing Chr is executed within the For...Next loop, another letter is appended to the Msg variable.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim I, J, Msg, NL                     ' Declare variables.

   NL = Chr(10)                          ' Define newline.

   For I = 1 to 2                        ' Set 2 iterations.

      For J = Asc("A") To Asc("Z")       ' From A through Z.

         Msg = Msg & Chr(J)              ' Create a string.

      Next J

   Msg = Msg & NL                        ' Insert newline.

   Next I

   MsgBox Msg                            ' Display results.

End Sub