Example 1
The code of a letter
C3 =CODE(A3)
Result: 97
Lower-case a is 97. Capital A is 65, so the two cases differ by 32.
C3 =CODE(A3) returns 97
Returns the number code of the first character in text, such as 65 for "A" or 32 for a space.
Syntax
=CODE()
All versions of Excel.
Try it
Edit the formula in C3 or change the inputs. The sheet recalculates as you type.
C3 =CODE(A3) returns 97
CODE returns the number that identifies the first character of text. It is the reverse of CHAR. It sounds academic, but it solves a common problem: finding out what an invisible character actually is.
=CODE("A") returns 65, =CODE("a") returns 97, and =CODE(" ") returns 32. If you give CODE more than one character, it only looks at the first: =CODE("Apple") returns 65.
An empty string, or an empty cell, returns #VALUE!.
When a lookup fails and the text looks right, the problem is often an invisible character at the start or end. Take the last character with RIGHT, or the first with LEFT, and ask for its code:
=CODE(RIGHT(A2,1))
The answer tells you what you are dealing with:
| Code | Character | |---|---| | 32 | Ordinary space (TRIM removes it) | | 160 | Non-breaking space (TRIM can't remove it) | | 9 | Tab | | 10 | Line break | | 13 | Carriage return |
Once you know the code, you can fix it: a code of 10 or 9 is removed by CLEAN, a code of 32 by TRIM, and a code of 160 by SUBSTITUTE(A2,UNICHAR(160)," "). To check any position, use MID: =CODE(MID(A2,3,1)).
Since letters have ordered codes, CODE lets you test what kind of character you have. Capital letters are 65 to 90 and lower-case letters are 97 to 122. =AND(CODE(A2)>=65,CODE(A2)<=90) returns TRUE if the first character of A2 is a capital letter. Digits are 48 to 57.
CODE converts a letter to a number you can calculate with, for example to find a letter's position in the alphabet: =CODE(UPPER(A2))-64 returns 1 for A, 2 for B and so on.
CODE uses the character set of your computer, which gives reliable results up to 127. Above that the numbers differ between Windows and Mac. For characters outside basic Latin, such as accented letters, symbols or emoji, use UNICODE. It returns the Unicode code point, which is the same on every computer. The matching function for the other direction is UNICHAR.
CODE returns a number, so you can compare it or calculate with it. It doesn't convert a whole text: to see the codes of every character, you need an array formula, for example =CODE(MID(A2,SEQUENCE(LEN(A2)),1)) in Microsoft 365.
#VALUE!.Each example is a live sheet. Change the inputs or the formula and watch the result.
Example 1
C3 =CODE(A3)
Result: 97
Lower-case a is 97. Capital A is 65, so the two cases differ by 32.
C3 =CODE(A3) returns 97
Example 2
C2 =CODE(RIGHT(A2,1))
Result: 10
Code 10 is a line break. CLEAN removes it.
C2 =CODE(RIGHT(A2,1)) returns 10
Example 3
C2 =CODE(MID(A2,6,1))
Result: 160
The gap in A2 looks like a space, but its code is 160, not 32. TRIM leaves it alone, and SUBSTITUTE with UNICHAR(160) fixes it.
C2 =CODE(MID(A2,6,1)) returns 160
Example 4
C3 =AND(CODE(A3)>=65,CODE(A3)<=90)
Result: FALSE
The first character of A3 is a lower-case r, code 114, which is outside 65 to 90. The same formula in C2 returns TRUE.
C3 =AND(CODE(A3)>=65,CODE(A3)<=90) returns FALSE
Solve it in the sheet. Every answer is checked, with hints if you need them.
Task 1 of 2
In B2, return the character code of the last character of the code in A2. Fill it down to B5.
Answer in B2, then it fills down to B5.
#VALUE!. Guard it with IF(A2="","",CODE(A2)).Take it out with LEFT, MID or RIGHT and pass it to CODE: `=CODE(RIGHT(A2,1))`. A result of 32 is a normal space, 160 a non-breaking space, 10 a line break and 9 a tab.
CODE uses the character set of your computer and is reliable up to 127. UNICODE returns the Unicode code point, which is the same on every computer and covers all characters.
Use `=CODE(UPPER(A2))-64`. A returns 1, B returns 2, and so on up to Z, which returns 26.
The text is empty. CODE needs at least one character to read. Check that the cell isn't blank.
Use `=AND(CODE(A2)>=65,CODE(A2)<=90)`. It returns TRUE for capital letters A to Z. For a case-sensitive comparison of whole values, use EXACT.
Tracks take you from the basics to modelling, one graded exercise at a time.