MS Excel: CODE function to return numeric code for the first character
- Fakhriddinbek
- 4 days ago
- 2 min read
The CODE function in Excel returns the numeric code for the first character in a text string. In most cases, this will be the ASCII code (on Windows systems) or Unicode code point (on some platforms) of that character. It is often used when analyzing, comparing, or transforming text data—especially in cases where characters must be interpreted programmatically.

If you’ve ever needed to check the underlying character value of a letter, symbol, or space—this is the function to use.
Syntax
=CODE(text)
Parameters:
text (required): The text string from which to return the numeric code of the first character.
The CODE function takes a string (or a cell containing text) and returns the numeric value of its first character only.
For example:
"A" returns 65
"a" returns 97
" " (space) returns 32
"1" returns 49
Examples
Example 1: Get ASCII Code of a Character
=CODE("A")
Result: 65(because "A" corresponds to ASCII code 65)
Example 2: Get Code from Cell Text
If cell A1 contains the word Excel, then:
=CODE(A1)
Result: 69(because the first character, "E", corresponds to ASCII code 69)
Example 3: Compare Two Characters
=CODE("A") = CODE("a")
Result: FALSE(Because ASCII of "A" is 65, and "a" is 97)
Practical Use Cases
Scenario | Application |
Sorting or classifying text | Use CODE to detect types of characters (e.g., uppercase, lowercase, special symbols) |
Data validation | Detect unexpected characters in data (e.g., invalid leading symbols) |
Character encoding tasks | Understand and manipulate character-based encoding logic |
Debugging invisible characters | Combine with CLEAN or UNICODE to find problematic hidden characters |
Summary
Feature | Detail |
Function Name | CODE |
Purpose | Returns ASCII (or Unicode) code of the first character in a string |
Output | Integer |
Common Use | Data validation, debugging, character manipulation |
Notes and Limitations
Only returns the code of the first character in the string.
In Unicode-enabled environments, CODE may return Unicode points rather than standard ASCII, though this mainly affects Mac users.
If the cell is empty or the input is a null string (""), Excel returns a #VALUE! error.
Related Functions
Function | Description |
CHAR | Returns a character based on a numeric code |
UNICODE | Returns the Unicode code point of a character |
LEFT | Returns the first character(s) of a string |
CLEAN | Removes non-printable characters from a string |
Final Thoughts
The CODE function is a simple yet powerful tool for anyone working with text data in Excel. It is especially useful for character analysis, debugging encoding issues, and creating custom formulas involving text comparisons or validations.
Whether you're verifying input data or manipulating text strings at the byte level, CODE gives you the insight into the numeric foundation behind every character.
Comments