Text
CODE Function in Excel
Returns the numeric code for the first character in a text string.
Syntax
- =CODE(text)
Arguments
- text (required): Text to get first character code from
Examples
- =CODE("A") - Code for letter A - Result: 65
- =CODE("a") - Code for lowercase a - Result: 97
Excel CODE function documentation
- CODE returns the numeric code for the first character in a text string.
- Syntax: =CODE(text). Useful for diagnosing hidden character issues in imported data.
- Use CODE to troubleshoot lookup mismatches and non-printable prefix characters.
- Text function category: [/categories/text/](/categories/text/).
CODE syntax and first-character focus
- text is required, and CODE evaluates only the first character.
- Use LEFT, MID, or SUBSTITUTE to isolate other positions before CODE checks.
- Legacy character-set behavior can vary by platform and language settings.
- Pair with CLEAN and TRIM to fix detected control-character problems.
Step-by-step: detect hidden leading characters
- Step 1 - Suspect values in column A where lookups fail unexpectedly.
- Step 2 - In B2 use =CODE(A2).
- Step 3 - Compare with expected normal codes such as letters or digits.
- Step 4 - Clean source with TRIM(CLEAN(A2)) and retest lookup results.
CODE vs UNICODE vs CLEAN
- CODE inspects first-character code in legacy encoding contexts.
- UNICODE is preferred for modern Unicode-aware diagnostics.
- CLEAN removes many non-printable control characters after detection.
- Display checks can include [TEXT](/functions/text/) for debug labels.
Worked examples to copy
- First-character code: =CODE(A2).
- Check first char of trimmed text: =CODE(TRIM(A2)).
- Classify printable range: =IF(CODE(A2)>=32,"Printable","Control").
- Audit with char preview: =CODE(A2)&"-"&LEFT(A2,1).
People also ask
- Why does CODE show unexpected values? - Hidden characters may exist at string start.
- CODE or UNICODE for multilingual data? - UNICODE is safer for modern datasets.
- Can CODE inspect the second character? - Yes, wrap MID(text,2,1).
- Will CODE fix data automatically? - No, it helps diagnose issues before cleanup.
Common errors
- Returns code for first character only
Use cases
- Character analysis
- Sorting logic
- Data validation
Frequently asked questions
- How do I check if a cell starts with a letter vs a number? Use CODE to check the first character: =IF(AND(CODE(A1)>=65, CODE(A1)<=90), "Uppercase", IF(AND(CODE(A1)>=97, CODE(A1)<=122), "Lowercase", IF(AND(CODE(A1)>=48, CODE(A1)<=57), "Number", "Other"))).
- What is the difference between CODE and UNICODE? CODE returns ASCII values (0-255), suitable for basic Latin characters. UNICODE returns full Unicode code points, supporting all characters including emojis and international scripts. Use UNICODE for non-ASCII characters.
- How do I get codes for all characters in a string? CODE only returns the first character's code. For all characters, use MID with CODE: =CODE(MID(A1, 1, 1)) for 1st, =CODE(MID(A1, 2, 1)) for 2nd, etc. Or create a helper column for each position.
Editorial review
- Reviewed by Excel.Directory Editorial Team. Updated May 2026.
When to use CODE
- Character analysis — common Text scenario for CODE.
- Sorting logic — common Text scenario for CODE.
- Data validation — common Text scenario for CODE.
CODE in the Text category
- Browse all Text functions at /categories/text/ for related formulas.
- CODE syntax: =CODE(text)
- text (required): Text to get first character code from
- Confirm CODE arguments match the syntax shown above before filling down.
- Lock table and range references with $ when copying formulas across rows or sheets.
Formula checklist before you copy down
- Confirm CODE arguments match the syntax shown above before filling down.
- Lock table and range references with $ when copying formulas across rows or sheets.
- If results look wrong, check for text stored as numbers and invisible spaces with TRIM.
- Spot-check three known input rows manually against expected output.
Related Excel functions
- CHAR (/functions/char/): Returns the character specified by a number (ASCII or Unicode code).
- UNICODE (/functions/unicode/): Returns the Unicode code point for the first character of a text string.
- UNICHAR (/functions/unichar/): Returns the Unicode character for a given number.
Errors to watch for
- Returns code for first character only — review causes on linked error pages in the directory.
Copy-paste audit workflow
- Enter CODE on three test rows with known expected output documented on a QA tab.
- Fill down only after absolute references are locked on lookup tables and rate tables.
- Compare against manual calculation or a calculator for financial and statistical functions.
- Search this directory for comparison guides when choosing between similar functions in the same category.
CODE worked examples to copy
- =CODE("A") — Code for letter A. Expected result: 65.
- =CODE("a") — Code for lowercase a. Expected result: 97.
CODE reference summary for crawlers and offline review
- CODE belongs to the Text category in Excel. Returns the numeric code for the first character in a text string.
- Full syntax: =CODE(text). Open /functions/code/ for parameters, FAQs, and related pages.
- Common mistakes: Returns code for first character only
- Pair this function with comparison guides when another Excel formula might fit the same task better.
- Review fix-excel-formula-errors when unexpected errors appear after upgrading Excel or sharing across locales.