# CODE function in Excel

> CODE returns the numeric code of the first character in a text string.

Source: https://spreadsheetpractice.com/functions/code · Updated October 28, 2026 · Formulas and results on this page were run by the Spreadsheet Practice formula engine on November 20, 2026

## Quick answer

- **What it does:** CODE returns the numeric code of the first character in a text string.
- **Syntax:** `=CODE(text)`
- **Most common use:** `=CODE(A2)` returns 65 (Capital A is code 65)
- **Available in:** Every current version
- **Category:** [Text functions](https://spreadsheetpractice.com/functions/category/text)
- **Returns:** A number: the code of the first character.

## What it does

CODE returns the numeric code of the first character in a text string. It's a diagnostic tool: when two cells look identical but won't match, CODE reveals hidden characters such as a nonbreaking space (160), a tab (9) or a line break (10).

## Syntax

`=CODE(text)`

| Argument | Required? | What it is |
|---|---|---|
| `text` | Required | The text whose first character you want to identify. |

## Examples

Each result was calculated by running the formula on the sample sheet.

| Cell | Formula | Result | What it shows |
|---|---|---|---|
| B2 | `=CODE(A2)` | 65 | Capital A is code 65 |
| B3 | `=CODE(A3)` | 97 | Lowercase a is 97, so case changes the code |
| B4 | `=CODE(A4)` | 32 | A leading space shows up as 32 |

### The sample sheet

Every formula above runs on this data:

|  | A | B |
|---|---|---|
| 1 | Text | Result |
| 2 | Apple | 65 |
| 3 | apple | 97 |
| 4 | Pear | 32 |

## Common mistakes

| What you see | Why, and how to fix it |
|---|---|
| #VALUE! | The cell is empty. Guard it: =IF(A2="","",CODE(A2)). |
| It only checks one character | CODE ignores everything after the first character. To check another position, combine it with MID: =CODE(MID(A2,5,1)). |

## Tips

- See what a trailing character really is with =CODE(RIGHT(A2)).

## Related functions

- [CHAR](https://spreadsheetpractice.com/functions/char): CHAR returns the character for a code number.
- [CLEAN](https://spreadsheetpractice.com/functions/clean): CLEAN removes non-printing characters, such as line breaks and tabs, from text.
- [TRIM](https://spreadsheetpractice.com/functions/trim): TRIM removes spaces from the start and end of text and shrinks runs of spaces between words to one.

## Questions

### How do I find hidden characters in Excel?

Check the first and last characters with =CODE(LEFT(A2)) and =CODE(RIGHT(A2)). 32 is a space, 160 a nonbreaking space, 10 a line break and 9 a tab.

### What is the opposite of CODE?

CHAR. =CHAR(65) returns A, and =CODE("A") returns 65.

## Practice

A graded CODE exercise ("Spot hidden characters") is on [the CODE page](https://spreadsheetpractice.com/functions/code). It is checked instantly, then re-checked on changed data.

---

Spreadsheet Practice (https://spreadsheetpractice.com) teaches spreadsheet formulas by practice: every exercise is graded in a live grid and re-checked on changed data.
