Chr 函数Chr function

返回一个字符串,其中包含与指定的字符代码关联的字符。Returns a String containing the character associated with the specified character code.



必需的 charcode 参数是用于确定字符的 LongThe required charcode argument is a Long that identifies a character.


0–31 之间的数字与标准非打印 ASCII 代码相同。Numbers from 0–31 are the same as standard, nonprintable ASCII codes. 例如,Chr(10) 将返回换行符。For example, Chr(10) returns a linefeed character. charcode 的正常范围是 0–255。The normal range for charcode is 0–255. 但是,在 DBCS 系统中,charcode 的实际范围是 -32768–65535。However, on DBCS systems, the actual range for charcode is -32768–65535.


[!注释] ChrB 函数与 字符串 中包含的字节数据一起使用。The ChrB function is used with byte data contained in a String. ChrB 总是返回一个字节,而不会返回一个字符(可能包含一个或两个字节)。Instead of returning a character, which may be one or two bytes, ChrB always returns a single byte.

ChrW 函数返回包含 Unicode 字符的字符串,但对不支持 Unicode 的平台例外,这种情况下的行为与 Chr 函数相同。The ChrW function returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function.


适用于 Macintosh 的 Visual Basic 不支持 Unicode 字符串。Visual Basic for the Macintosh does not support Unicode strings. 因此,ChrW(n) 无法返回 128–65,535 范围内的 n 值对应的所有 Unicode 字符,这与 Windows 环境中相同。Therefore, ChrW(n) cannot return all Unicode characters for n values in the range of 128–65,535, as it does in the Windows environment. 相反,ChrW(n) 会尝试“最佳猜测”大于 127 的 Unicode 值 n。Instead, ChrW(n) attempts a "best guess" for Unicode values n greater than 127. 因此,您不应在 Macintosh 环境中使用 ChrWTherefore, you should not use ChrW in the Macintosh environment.

函数 Asc()、AscB() 和 AscW()Chr()、ChrB() 和ChrW() 的反函数。The functions Asc(), AscB(), and AscW() are the opposite of Chr(), ChrB(), and ChrW(). Asc() 函数将字符串转换为整数。The Asc() functions convert a string to an integer.


此示例使用 Chr 函数返回与指定字符代码关联的字符。This example uses the Chr function to return the character associated with the specified character code.

Dim MyChar
MyChar = Chr(65)    ' Returns A.
MyChar = Chr(97)    ' Returns a.
MyChar = Chr(62)    ' Returns >.
MyChar = Chr(37)    ' Returns %.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.