← Functions
codetextfunctionsbeginner

Excel CODE Function

CODE returns the numeric ANSI code for a text string's first character. Covers syntax, examples, and how it compares to UNICODE.

CODE returns the numeric code for the first character in a text string. On a Windows PC, that number comes from the ANSI character set, so A returns 65 and a returns 97. The one thing that trips people up: CODE only ever looks at the first character. If your cell holds "Apple123," you'll get the code for "A" and nothing else.

Syntax

=CODE(text)
ParameterRequiredDescription
textYesThe text string, or a cell reference containing text, you want a code for. Only the first character is evaluated.

ANSI is the character encoding Windows uses to map letters, numbers, and symbols to numeric codes; it covers 256 values (0 to 255).

Basic Example

Say you're building a product code generator and need the numeric value behind the first letter of each item name.

=CODE(B2)

// B2  =  the text string, in this case "Widget"

CODE reads the first character of B2, which is "W," and returns 87. It ignores everything after the first letter entirely; "Widget" and "W" return the identical result.

How CODE Works

It only reads the first character

=CODE("Apple") returns 65, the same result as =CODE("A"). If you need the code for a character further into a string, you have to isolate it first with LEFT, MID, or RIGHT.

Case changes the result

Uppercase and lowercase letters have different codes. =CODE("A") returns 65, but =CODE("a") returns 97, a 32-point gap that holds true for the entire alphabet.

Numbers and symbols get coerced to text

=CODE(5) doesn't return the numeric value 5. Excel converts the number to the text string "5" first, then returns its code, 53. Spaces, punctuation, and symbols all work the same way: =CODE(" ") returns 32, =CODE("$") returns 36.

CHAR reverses it

If CODE turns a character into a number, CHAR turns a number back into a character. =CHAR(87) returns "W." You'll usually reach for CHAR when you need to insert a character that's hard to type directly into a formula, like a line break.

Common Use Cases

Getting the code for every character in a string

Neither CODE's built-in behavior nor most tutorials cover this, but a spill formula maps every character in a string to its code at once. Useful when auditing a cell for hidden or non-printable characters.

=CODE(MID(A2,SEQUENCE(LEN(A2)),1))

// A2  =  the text string to break apart
// SEQUENCE(LEN(A2))  =  generates 1, 2, 3... one number per character
// MID(...,1)  =  pulls one character at a time

This spills one code per row, letting you spot a stray tab or non-breaking space that a normal glance wouldn't catch.

Flagging entries that don't start with a letter

If you're validating a product ID field where entries should always start with a letter, not a digit or symbol:

=IF(CODE(A2)<65,"Check entry","OK")

// A2  =  the product ID being validated
// 65  =  the ANSI code for "A", anything below it is a digit or symbol

Bridging to VBA with Asc

If you're moving logic from a worksheet formula into a macro, CODE has a direct VBA equivalent, but it's not named the same thing. The VBA function Asc does exactly what CODE does on the worksheet: it returns the ANSI code for the first character of a string.

MsgBox Asc("Widget")   ' returns 87, same result as =CODE("Widget")

This mapping rarely gets spelled out clearly, so if you're translating a spreadsheet formula into VBA and searching for "CODE" in the VBA object browser, you won't find it. Look for Asc instead.

Building a sort key from the first letter

Combine CODE with other text functions to create a numeric sort key based on a name's first letter, useful when you need numeric rather than alphabetic sort logic downstream.

=CODE(UPPER(LEFT(A2,1)))

// LEFT(A2,1)  =  isolates the first character
// UPPER(...)  =  forces it to uppercase so sorting stays consistent

Handling Errors

CODE returns #VALUE! when there's no character to read.

Common causes of #VALUE!:

  • The referenced cell is blank
  • The text argument is an empty string ""
  • The argument references a cell containing only a formula error

Wrapping CODE in IFERROR isn't a good fix here. IFERROR would swallow any error the argument throws, including a broken reference or a typo elsewhere in the formula, not just the blank-cell case you're trying to handle. Test for the blank condition directly instead:

=IF(A2="", "", CODE(A2))   // returns "" for blank cells instead of #VALUE!

IF(A2="","",CODE(A2)) is the safer choice over IFERROR for this specific error. It prevents the error from firing at all instead of catching it after the fact, which keeps the formula honest about what it's actually handling and easier to audit later.

Notes & Gotchas

What is the CODE function in Excel?

CODE is a text function that returns a numeric code for the first character in a given text string. It's built into every version of Excel and works identically on the desktop and in Excel 365.

How do you use the CODE function in Excel?

Enter it as a worksheet formula, referencing a cell or typing the text directly: =CODE(A2) or =CODE("Hello"). It always returns a single number representing the first character only, regardless of how long the string is.

What is the difference between CODE and UNICODE functions in Excel?

CODE reports the legacy ANSI code, a number from 1 to 255, and works in every Excel version. Characters outside that range, like most emoji, come back as 63, the code for a generic question mark placeholder. UNICODE, available in Excel 2013 and later, reports the true Unicode code point for any character, emoji included, with no ceiling at 255.

How do you get the code of the last character in a cell?

Slice it out first, then feed the result to CODE: =CODE(RIGHT(A2,1)). RIGHT pulls the final character regardless of the string's length, and CODE returns its number.

Why does CODE return a different number than I expected?

The most common cause is case. "A" and "a" are 32 numbers apart (65 vs. 97), so a formula built for uppercase input will quietly return the wrong result on lowercase text. Force consistent case first with UPPER or LOWER before feeding text to CODE.

CODE ignores every character after the first one. CODE("Excel") and CODE("E") return the identical value, 69. If you need a code from further into the string, isolate that character with LEFT, MID, or RIGHT before calling CODE, otherwise you'll silently get the wrong character's code.

Related Functions

FunctionUse this when...
MIDYou need to isolate a character from the middle of a string before passing it to CODE.
CHARYou have the numeric code and need the character it represents, the reverse of what CODE does.