
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.
That makes CODE especially helpful in cleanup and validation work. If a lookup fails, a comparison behaves strangely, or imported text seems inconsistent, CODE can reveal whether the first character is actually a space, a line break, a number, or a letter with different case than you expected. In many real worksheets, the problem is not the whole cell value but one hidden character at the start.
CODE is also best understood as the opposite of CHAR. CODE reads a character and gives you its number, while CHAR takes a number and gives you the character back. Together they help you inspect, explain, and fix character-level problems in text data.
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 C1, use CODE to find the number for the letter in B1.
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 C2, 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 C3, 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 D4, use CODE to compare the letter in B4 against a minimum code in C4.
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 is mainly a diagnosis function. This lesson showed that it helps when a text value looks fine but the first character is actually something different, like a hidden line break, a leading space, or the wrong letter case.
The key thing to remember is that CODE only checks the first character unless you combine it with something like MID. Once you know the character code causing the problem, it becomes much easier to clean or replace it with CHAR, SUBSTITUTE, or another text function.
CODE returns the code for the first character in a text string.=CODE(text).Tell your friends about this post