VBA Chr
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
Excel VBA CHR Function
VBA CHR is a built-in function that falls under the String/Text Functions category used to get the character equivalent to the ASCII code. For example, the equivalent Excel Workspace function of CHR is Application.WorksheetFunction.CHAR.
Table of contents
Given below is the Chr syntax.
This function has one argument. Where,
Charco de = This is a required parameter. It is the ASCII code for which an equivalent character is to be retrieved.
The function returns a string value denoting the character equivalent to the given ASCII code. ASCII is a subset of the Unicode character coding standard formed by 128 symbols in the character set, including uppercase, lowercase letters, numbers, special characters, control characters, and punctuation marks. In addition, every symbol in the character set has an equivalent Decimal value (0 to 127), a Hexadecimal value, and an Octal value.
Example
Step 1: In the Excel sheet, add two header texts in cells A1 and C1, as shown in the figure below. Column A to enter the ASCII code and column C to print the corresponding character calculated using the CHR function.
Step 2: Follow the steps in the next section to create a Button in the Excel workbook (please scroll the article) and change its caption to "Click here."
Step 3: Write the following code snippet in VBA. The code reads the value from cell A2, which is given as an input to function CHR. The result is retrieved in String char1 and then assigned to cell C2.
Code:
Sub Button1_Click() 'This function returns the character for the value entered in cell A2 Dim char1 As String 'Declare char1 variable as String char1 = Chr(Range("A2").Value) 'Read value from cell A2 Range("C2").Value = char1 'Print output in cell C2 End Sub
Step 4: Save the VBA Excel code and go back to the Excel workbook to enter the input value in cell A2, as shown below.
Enter 65 as an ASCII input for the corresponding character to be found in cell C2.
Step 5: Click the "Click here" button to print the result in cell C2.
Observe the result printed in cell C2. The code snippet we wrote in step 3 is responsible for reading the input from cell A2, running the Chr function on it, and printing the value in cell C2, as shown below.
Here, the input given is 65, and the output received is A. So, Chr (65) = A.
Step 6: Try changing the input in cell A2 and observe that you get the respective output in cell C2, as shown below.
E.g. CHR (37) = % and so on.
Things to Remember
- The CHR function can return printable and non-printable characters present on the keyboard and understood by the computer. E.g., letters, numbers, and other special characters are printable characters. However, other keys such as "Enter," "Spacebar," and "Esc" are non-printable characters.
- The CHR is a VBA function and cannot be used in Excel as it is. Its corresponding function in Excel is Application.WorksheetFunction.CHAR.
Recommended Articles
This article has been a guide to VBA CHR. Here we discuss how to get a character of the ASCII code in Excel using the VBA CHR function along with examples and codes. You can learn more about VBA from the following articles: -