CODE Function

Excel Functions › Text

All versions Text

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.


Quick answer: to see the code of the first character in A2:
=CODE(A2)
=CODE("A") returns 65. To inspect the last character instead — the usual hiding spot for junk — use =CODE(RIGHT(A2,1)).

Syntax

=CODE(text)
ArgumentDescription
textRequiredThe 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:

ABC
1Pasted text=CODE(LEFT(A2,1))Verdict
2␣Dallas160non-breaking space — TRIM won’t touch it
3␣Dallas32normal space — TRIM removes it
=CODE(LEFT(A2,1)) // 160 = non-breaking space, 32 = real space

The codes you’ll meet most often while debugging:

CodeCharacterHow it gets in
32normal spaceThe space bar. Harmless.
160non-breaking spaceCopy-paste from websites. TRIM ignores it — remove with SUBSTITUTE.
10line breakAlt+Enter, or imports. Remove with CLEAN.
13carriage returnFiles 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:

=CODE(RIGHT(A2,1)) // 32 or 160 here explains a failed exact match

And to walk through a string one position at a time, pair it with MID:

=CODE(MID(A2, 3, 1)) // code of the 3rd character

Try it: interactive CODE demo

Live 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

📊
Download the free CODE practice workbook
Every example on this page, ready to open in Excel — plus practice challenges with answers on a separate tab. No sign-up required.

Frequently asked questions

What's the difference between CODE and UNICODE?
CODE reports the legacy ANSI code (1–255) and works in every Excel version; characters outside that set come back as 63. UNICODE (Excel 2013+) reports the true Unicode code point of any character — emoji included.
Why does CODE return 63 for my character?
63 is the code for the question mark. When a character has no slot in the ANSI table - an em dash, a curly quote from Word, an emoji - CODE substitutes "?" and reports its code. Use UNICODE to see the character's real code point.
How do I get the code of the last character in a cell?
Slice it out first: =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?
The text argument is empty — a blank cell or "". There’s no first character to read. Wrap it: =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

Related functions: CHAR · UNICODE · UNICHAR · TRIM · CLEAN · SUBSTITUTE