
Returns the numeric code for the first character in a text string.
The Excel CODE function returns the numeric code for the first character in a text string. For example, =CODE("A") returns 65.
CODE is useful when you need to identify a character that is hard to notice, such as a leading space, a line break, or another unexpected symbol. It only looks at the first character, so it is often used as a diagnostic tool rather than a display function.
Returns the numeric code for the first character in the text.
Returns the code for the first character only.
=CODE(text)
text is the string or cell reference you want to inspect. CODE reads only the first character and ignores the rest of the string.
CODE and CHAR work as opposites. CODE gives you the number for a character, and CHAR turns that number back into the character. CLEAN, TRIM, and SUBSTITUTE often appear in the next step, after CODE helps identify the problem.
| Function | Main Role | Use When |
|---|---|---|
CODE |
Character to code | You want to identify the first character numerically |
CHAR |
Code to character | You want to recreate a character from its code |
TRIM |
Remove extra spaces | You suspect spacing issues |
SUBSTITUTE |
Replace a chosen character | You know which character should be removed or changed |
CODE is often used when a value looks correct on screen but still behaves differently in a lookup or comparison. If the first character is a hidden line break or an extra space, CODE can reveal it.
It is also useful when case matters. Uppercase and lowercase letters have different codes, so CODE can show that "A" and "a" are not treated the same.
This is the direct use of CODE. The first character is converted into its numeric code.
=CODE("A") // 65
=CODE("a") // 97
=CODE("1") // 49
In cell F1, use CODE to find the number for the letter in B1. B1 contains "A". Expected result: 65.
Uppercase and lowercase versions of the same letter do not share the same code. That makes CODE useful when a column should follow one case rule.
=CODE("A") // 65
=CODE("a") // 97
In cell F2, use CODE on B2 to find the number for "a" (lowercase). Notice it is different from "A". Expected: 97.
If a cell begins with an invisible character, CODE can reveal it. A result of 10 often means a line break, and 32 means a regular space.
=CODE(B3)
// If B3 starts with a line break, the result is 10
In cell F3, use CODE on B3 to reveal the code of the first hidden character. Expected: 10.
CODE can also support simple rules. In this example, the formula checks whether the code of the first character meets a minimum value stored in another cell.
=IF(CODE(B4)>=C4, "Pass", "Fail")
In cell F4, use CODE to check if the letter in B4 meets the minimum code stored in C4. Formula: =IF(CODE(B4)>=C4,"Pass","Fail").
If the character you need is not at the start of the string, combine CODE with MID. For example, =CODE(MID(A1,3,1)) checks the third character instead of the first.
CODE returns the code for the first character in a text string.=CODE(text).Tell your friends about this post