The VBA Chr function is used to return the character associated with a specific character code. This function is extremely useful when working with strings and character manipulation in VBA. The function takes in the character code as an argument and returns the corresponding character. It can also be used in conjunction with the ‘Asc’ function to convert characters to their corresponding character codes.
VBA Chr Function – Purpose, Syntax and Arguments
- Charcode: This is the required argument for the Chr function and it represents the character code for which the corresponding character needs to be returned. The character code can be any integer value between 0 and 255.
Suppose we want to create a string that displays the alphabet in uppercase letters. We can use the Chr function inside a loop to achieve this as shown in the following code:
Dim i As Integer
Dim uppercase As String
For i = 65 To 90
uppercase = uppercase + Chr(i)
The resulting string displayed in the message box will be: ABCDEFGHIJKLMNOPQRSTUVWXYZ.
- The Chr function only works with character codes in the range of 0 to 255. Any character code outside this range will result in an error.
- The function can also be used to insert special characters in strings, such as tab, line feed, and carriage return, by using their respective ASCII codes.
- If the Chr function is used with a character code that is outside the ASCII character set, it will return a non-printable character.
- The Chr function is not limited to just VBA, it can also be used in other programming languages such as VB.NET and C#.
- The function can be used with the ‘Asc’ function to convert a character to its corresponding character code, and vice versa.
- The Chr function is case-sensitive, meaning that a different character will be returned for uppercase and lowercase character codes.
- The character codes used by the Chr function are based on the American Standard Code for Information Interchange (ASCII) character set, which is the most commonly used character encoding system.
Understanding VBA Chr Function with Examples
Description: The Chr function is a VBA built-in function that returns a character based on its corresponding ASCII code. It is used to convert numeric values to their equivalent characters.
Explanation: The Chr function takes in one argument, which is the ASCII code corresponding to the character you want to retrieve. In this example, the ASCII code for the letter A is 65, so the function returns the character A as its output.
Description: Apart from the ASCII code, the Chr function can also take in multiple arguments to create a string of characters. It can be used to create special characters string for formatting purposes.
Chr(firstcode, secondcode, thirdcode, ...)
Chr(65, 66, 67)
Explanation: In this example, the Chr function takes in three arguments – 65, 66, and 67, which are the ASCII codes for the letters A, B, and C respectively. When multiple codes are provided, the function concatenates the characters together and returns them as a string.
Using in a Loop
Description: The Chr function can be used in conjunction with loops to create a string of characters that follow a pattern or increase in value.
For i = 65 To 69
str = str & Chr(i)
Explanation: In this example, the Chr function is used within a For loop to dynamically create a string of characters by increasing the ASCII code by 1 in each iteration. The variable str is first assigned an empty string, and then the Chr function is used to add each character to the string. After the loop finishes executing, the variable str will contain the characters A, B, C, D, and E.
Using with User Input
Description: The Chr function can also be used with user input to retrieve characters based on the ASCII codes entered.
Dim code As Integer
code = InputBox("Enter an ASCII code:")
Enter an ASCII code:
Explanation: In this example, the Chr function is used to convert the user input (ASCII code) into its equivalent character. The user is prompted to enter an ASCII code, and the function converts it to the character A. This can be useful when working with input fields in forms or when creating password fields.
Using with Built-in Functions
Description: The Chr function can be used in conjunction with other built-in VBA functions to manipulate and retrieve specific characters from strings.
Mid(Chr(65), 1, 1)
Explanation: In this example, the Chr function is used as an argument for the Mid function, which is used to retrieve a specific number of characters from a string. In this case, the Mid function is retrieving the first character from the character A returned by the Chr function.
The Chr function is a powerful and versatile function in VBA that allows you to retrieve characters based on their ASCII codes. It can be used for various purposes, such as creating strings, formatting, and working with user input. By understanding the basic syntax and examples of the Chr function, you can effectively utilize it in your VBA projects.