Excel CODE Function: Syntax, Examples and Tips

Part of the free Module 5: Excel Formulas and Functions · Function 11 of 105 · Full Excel course

The Excel CODE function returns the numeric code of the first character in a text string, using the character set of your computer (Windows-1252 on Windows, Macintosh on Mac). It is the reverse of CHAR: CODE(“A”) returns 65 and CHAR(65) returns A. Use it to identify hidden or unusual characters in imported data.

Syntax

=CODE(text)

Arguments

Argument Required / Optional Meaning
text Required The text whose first character you want to inspect. Only the first character is evaluated; the rest of the string is ignored.

How CODE behaves

  • CODE is case-sensitive because upper-case and lower-case letters have different codes: =CODE("A") returns 65, =CODE("a") returns 97.
  • Digits also have codes: =CODE("7") returns 55. If you pass a real number, Excel converts it to text first, so =CODE(7) also returns 55.
  • An empty string returns #VALUE!, so wrap CODE in IFERROR when the source cell may be blank.
  • Codes above 255 cannot be represented; for symbols and emoji use UNICODE.

Step-by-step example: find the invisible character

  1. A customer list imported from a website has names that fail in VLOOKUP. Cell A2 appears to contain “Smith” but =LEN(A2) returns 6.
  2. In B2 enter =CODE(LEFT(A2,1)). It returns 160, the non-breaking space, so the name really starts with an invisible character.
  3. In C2 enter =CODE(RIGHT(A2,1)) to check the last character as well.
  4. Fix the column with =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). The lookups now succeed.

Practical use cases

1. Test whether a cell starts with an upper-case letter

=AND(CODE(A2)>=65,CODE(A2)<=90)

2. Check whether the first character is a digit

=AND(CODE(A2)>=48,CODE(A2)<=57)

Useful for validating product codes that must begin with a number.

3. List the code of every character in a string

=CODE(MID($A$2,ROW(A1),1))

Fill this down as many rows as the text has characters. Any value below 32 or equal to 160 points to a character you need to remove.

4. Shift letters by a fixed offset (simple cipher)

=CHAR(CODE(A2)+1)

Turns A into B, B into C and so on. Combine with MID and TEXTJOIN to encode a whole word.

Common mistakes and tips

  • #VALUE! on blank cells: CODE cannot evaluate an empty string. Use =IF(A2="","",CODE(A2)).
  • Only the first character counts: =CODE("Excel") returns 69 for E. Use MID to reach later characters.
  • Different results on Mac: codes 128 to 255 differ between the Windows and Macintosh character sets. UNICODE gives the same answer everywhere.
  • Numbers are converted to text: CODE(10) does not return the line-feed code; it returns 49, the code of the digit 1. Use CODE(CHAR(10)) if you need to test a control character.

Character codes worth remembering

A few codes come up repeatedly in data-cleaning work. Knowing them lets you read CODE results without a reference table.

Code Character Where it appears
9 Tab Text pasted from web tables or text files
10 Line feed Alt+Enter line breaks inside Excel cells
13 Carriage return Data from Mac or mainframe systems
32 Space Normal spacing
48 to 57 0 to 9 Digits
65 to 90 A to Z Upper-case letters
97 to 122 a to z Lower-case letters
160 Non-breaking space Copied from web pages; ignored by TRIM

If CODE returns 63, the code of a question mark, for a character that clearly is not one, the character lies outside the local 8-bit set and CODE cannot represent it. Switch to UNICODE, which returns the true code point for any character.

Related functions

  • CHAR: converts a code back to its character.
  • UNICODE: returns the Unicode code point of the first character, for any language or symbol.
  • MID: extracts a character from any position so CODE can inspect it.
  • CLEAN: removes the control characters that CODE helps you find.
  • See all lessons in the Excel Formulas course.

Frequently asked questions

What does the CODE function return for a space?

A normal space returns 32. A non-breaking space copied from a web page returns 160, which is why TRIM cannot remove it.

What is the difference between CODE and UNICODE?

CODE returns a value from the local 8-bit character set (1 to 255). UNICODE returns the Unicode code point and works for every language, symbol and emoji.

Why does CODE return #VALUE!?

The text argument is an empty string, usually because the referenced cell is blank. Test for blanks with IF before calling CODE.

Need a ready-made template? Browse 7,000+ Excel, Power BI and Google Sheets templates at NextGenTemplates.com.