Skip to content

CODE

Returns the number code of the first character in text, such as 65 for "A" or 32 for a space.

Syntax

=CODE()

Text

All versions of Excel.

Try it

Edit the formula in C3 or change the inputs. The sheet recalculates as you type.

C3
A
B
C
1
Letter
Note
Code
2
A
Capital
3
a
Lower case
97
4

C3 =CODE(A3) returns 97

How CODE works

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.

What it returns

=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!.

Finding hidden characters

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)).

Testing a character type

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.

Letters in order

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.

Limits and Unicode

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.

Results are numbers

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.

Arguments

text
The text whose first character you want the code of. Only the first character is used. An empty string returns #VALUE!.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

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
A
B
C
1
Letter
Note
Code
2
A
Capital
3
a
Lower case
97
4

C3 =CODE(A3) returns 97

Example 2

Identify a hidden trailing character

C2 =CODE(RIGHT(A2,1))

Result: 10

Code 10 is a line break. CLEAN removes it.

C2
A
B
C
1
Pasted code
Owner
Last character code
2
FX-1010
Finance
10
3
4

C2 =CODE(RIGHT(A2,1)) returns 10

Example 3

Recognise a non-breaking space

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
A
B
C
1
Web export
Qty
Code of 6th character
2
Floor cleaner
24
160
3
4

C2 =CODE(MID(A2,6,1)) returns 160

Example 4

Test whether text starts with a capital letter

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
A
B
C
1
Note
Source
Starts with capital?
2
Rush order
Email
3
rush order
Phone
FALSE
4

C3 =AND(CODE(A3)>=65,CODE(A3)<=90) returns FALSE

Your turn.

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.

B2
A
B
C
D
1
Pasted code
Last char code
Owner
First letter code
2
FX-1010
Finance
3
FX-1085
Planning
4
FX-2040
Ops
5
FX-3012
Ops
6
7

Common mistakes

  • CODE reads only the first character. To check another position, take it out with LEFT, MID or RIGHT first.
  • An empty cell or empty string returns #VALUE!. Guard it with IF(A2="","",CODE(A2)).
  • Codes above 127 differ between Windows and Mac. Use UNICODE for accented letters, symbols and anything outside basic Latin.
  • CODE returns a number, but the character it comes from is still text. A digit such as "5" has the code 53, not 5.
  • A space looks the same whether it is code 32 or 160. Check the code before assuming TRIM will fix it.

Questions people ask

How do I find out what an invisible character is?

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.

What is the difference between CODE and UNICODE?

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.

How do I get the position of a letter in the alphabet?

Use `=CODE(UPPER(A2))-64`. A returns 1, B returns 2, and so on up to Z, which returns 26.

Why does CODE return #VALUE!?

The text is empty. CODE needs at least one character to read. Check that the cell isn't blank.

How do I check whether the first letter is a capital?

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.

Learn CODE in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.