Text function

CODE function

Returns the numeric code of the first character in a text string, e.g. CODE("A") = 65. Useful for finding hidden characters.

Available in All versions of Excel, including Microsoft 365, Excel for Mac and Excel for the web

In every Excel versionDifferent in Google Sheets

Syntax

CODE(text)
ArgumentWhat it means
textRequiredThe text whose first character you want the code of.

Returns: A number from 1 to 255 (the character's code in your system's character set).

How CODE works

CODE is the reverse of CHAR: give it some text and it tells you the code number of the first character. =CODE("A") returns 65 and =CODE("a") returns 97 - the difference shows that upper and lower case are completely different characters to the computer.

Its main practical use is detective work. When two cells look identical but a lookup or comparison says they differ, CODE reveals invisible characters: a normal space is 32, a non-breaking space from a web page is 160, a line feed is 10, a tab is 9. Combine CODE with LEFT, RIGHT or MID to inspect any position of a string.

On Windows CODE uses the Windows-1252 character set, so characters outside it (for example Greek or Chinese letters) return 63, the code of ?. Use UNICODE when you need the true Unicode number.

Examples

1Code of the first letter

B2fx=CODE(A2)
AB
1Name
2Maria77

Result: 77

Only the first character, capital M, is examined. Its code is 77.

2Find a hidden character at the end of a product code

B2fx=CODE(RIGHT(A2))
AB
1Product code
2PX-200 160

Result: 160

The code looks normal, but its last character is 160, a non-breaking space copied from a web page. TRIM will not remove it; use SUBSTITUTE(A2,CHAR(160),"").

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 13 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!The cell is empty or the text is "".Wrap it: =IF(A2="","",CODE(A2)).

Tips

  • To list the code of every character in A2 in one go (Microsoft 365): =CODE(MID(A2,SEQUENCE(LEN(A2)),1)) spills one code per row.
  • CODE("€") returns 128 on Windows; on other platforms the number may differ, so prefer UNICODE for symbols.

Google Sheets

Google Sheets returns the Unicode code point, so for characters outside basic ASCII the result can differ from Excel for Windows (ANSI code page).

CODE in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
CzechKÓD
DanishKODE
FinnishKOODI
HungarianKÓD
ItalianCODICE
Norwegian (Bokmål)KODE
PolishKOD
Portuguese (Brazil)CÓDIGO
Portuguese (Portugal)CÓDIGO
RussianКОДСИМВ
SpanishCODIGO
SwedishKOD
TurkishKOD