The Excel CODE function returns the numeric code (ANSI, 1–255) of the first character of a text string. It is the inverse of CHAR — and it earns its keep as a detective: when two cells look identical but a lookup or comparison says they aren’t, CODE unmasks the invisible character causing the trouble. For characters beyond the ANSI set (emoji, symbols, anything fancy), use UNICODE.
=CODE("A") returns 65. To inspect the last character instead — the usual hiding spot for junk — use =CODE(RIGHT(A2,1)).
Syntax
| Argument | Description | |
|---|---|---|
text | Required | The text to inspect. Only the first character counts — the rest of the string is ignored. An empty string returns #VALUE!. |
Available in: every version of Excel. CODE and CHAR are mirror images: =CHAR(CODE("A")) gives "A" back, and =CODE(CHAR(65)) gives 65 back.
Unmasking hidden characters
Two cells that both look like they start with a space — but VLOOKUP matches one and not the other. Point CODE at the first character and the impostor confesses:
| A | B | C | |
|---|---|---|---|
| 1 | Pasted text | =CODE(LEFT(A2,1)) | Verdict |
| 2 | ␣Dallas | 160 | non-breaking space — TRIM won’t touch it |
| 3 | ␣Dallas | 32 | normal space — TRIM removes it |
The codes you’ll meet most often while debugging:
| Code | Character | How it gets in |
|---|---|---|
32 | normal space | The space bar. Harmless. |
160 | non-breaking space | Copy-paste from websites. TRIM ignores it — remove with SUBSTITUTE. |
10 | line break | Alt+Enter, or imports. Remove with CLEAN. |
13 | carriage return | Files from other systems. Also a CLEAN job. |
63 | ? | Often means the real character is outside the ANSI set — switch to UNICODE to see its true code. |
To check the end of a string (trailing junk is the classic lookup-killer), aim CODE through RIGHT:
And to walk through a string one position at a time, pair it with MID:
Try it: interactive CODE demo
Type or paste anything — a letter, a symbol, a whole word — and see what CODE returns for its first character.
Errors & common pitfalls
Pitfall: only the first character is read. =CODE("Apple") returns 65 — the code for "A" — and says nothing about the other four letters. To inspect a different position, slice first with LEFT, RIGHT, or MID.
#VALUE! — empty text. =CODE("") and =CODE(A2) with a blank A2 both fail: there is no first character to report. Guard with =IF(A2="", "", CODE(A2)).
Pitfall: a suspicious 63. CODE speaks only ANSI (1–255). Feed it a character outside that set — an em dash, a checkmark, an emoji — and it typically reports 63, the code for "?". That answer is a shrug, not a reading. UNICODE returns the real code point.
Pitfall: codes 128–159 vary by platform. That band of the ANSI table differs between Windows and other systems, so results there aren’t portable. UNICODE sidesteps the whole issue.
Practice workbook
Frequently asked questions
What's the difference between CODE and UNICODE?
Why does CODE return 63 for my character?
How do I get the code of the last character in a cell?
=CODE(RIGHT(A2,1)). This is the go-to check when a lookup fails on text that "looks fine" — a trailing 160 (non-breaking space) or 32 (space) is usually the culprit.How do I tell a real space from a non-breaking space?
=CODE(A2) on the character in question: 32 is a normal space, 160 is the non-breaking kind that TRIM ignores. Remove 160s with =SUBSTITUTE(A2, CHAR(160), " "), then TRIM.Why does CODE give me a #VALUE! error?
=IF(A2="", "", CODE(A2)).Master functions like this in one day
This page covers one function. Our Excel Formulas and Functions class covers the 30 that matter most — live, hands-on, taught by professionals in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.
See the Formulas & Functions Class