CODE Function
CODE Function

CODE Function

Returns the numeric code for the first character in a text string.

ExcelClash Team
PUBLISHED

Summary

The Excel CODE function returns the numeric code for the first character in a text string. For example, =CODE("A") returns 65.

CODE is useful when you need to identify a character that is hard to notice, such as a leading space, a line break, or another unexpected symbol. It only looks at the first character, so it is often used as a diagnostic tool rather than a display function.

That makes CODE especially helpful in cleanup and validation work. If a lookup fails, a comparison behaves strangely, or imported text seems inconsistent, CODE can reveal whether the first character is actually a space, a line break, a number, or a letter with different case than you expected. In many real worksheets, the problem is not the whole cell value but one hidden character at the start.

CODE is also best understood as the opposite of CHAR. CODE reads a character and gives you its number, while CHAR takes a number and gives you the character back. Together they help you inspect, explain, and fix character-level problems in text data.

Purpose

Convert a character to its code

Returns the numeric code for the first character in the text.

Return Value

A whole number

Returns the code for the first character only.

Syntax

=CODE(text)

text is the string or cell reference you want to inspect. CODE reads only the first character and ignores the rest of the string.

Arguments

  • text - [Required] The text string or cell reference whose first character you want to identify.

CODE vs Other Functions

CODE and CHAR work as opposites. CODE gives you the number for a character, and CHAR turns that number back into the character. CLEAN, TRIM, and SUBSTITUTE often appear in the next step, after CODE helps identify the problem.

Function Main Role Use When
CODE Character to code You want to identify the first character numerically
CHAR Code to character You want to recreate a character from its code
TRIM Remove extra spaces You suspect spacing issues
SUBSTITUTE Replace a chosen character You know which character should be removed or changed

Using CODE

CODE is often used when a value looks correct on screen but still behaves differently in a lookup or comparison. If the first character is a hidden line break or an extra space, CODE can reveal it.

It is also useful when case matters. Uppercase and lowercase letters have different codes, so CODE can show that "A" and "a" are not treated the same.

  • Use CODE when a hidden first character may be causing a problem.
  • Use CODE with CHAR and SUBSTITUTE when you need to identify and then remove a specific character.
  • Use CODE with MID if the character you need to inspect is not the first one.

Example 1 - Checking a Single Character

This is the direct use of CODE. The first character is converted into its numeric code.

=CODE("A") // 65
=CODE("a") // 97
=CODE("1") // 49
Check Answer
Challenge #1
Target: Sheet1!C1

In cell C1, use CODE to find the number for the letter in B1.

Example 2 - Spotting the Difference Between Cases

Uppercase and lowercase versions of the same letter do not share the same code. That makes CODE useful when a column should follow one case rule.

=CODE("A") // 65
=CODE("a") // 97
Check Answer
Challenge #2
Target: Sheet1!C2

In cell C2, use CODE on B2 to find the number for "a" (lowercase). Notice it is different from "A". Expected: 97.

Example 3 - Finding a Hidden Character

If a cell begins with an invisible character, CODE can reveal it. A result of 10 often means a line break, and 32 means a regular space.

=CODE(B3)
// If B3 starts with a line break, the result is 10
Check Answer
Challenge #3
Target: Sheet1!C3

In cell C3, use CODE on B3 to reveal the code of the first hidden character. Expected: 10.

Example 4 - Comparing Against a Target in Another Cell

CODE can also support simple rules. In this example, the formula checks whether the code of the first character meets a minimum value stored in another cell.

=IF(CODE(B4)>=C4, "Pass", "Fail")
Check Answer
Challenge #4
Target: Sheet1!D4

In cell D4, use CODE to compare the letter in B4 against a minimum code in C4.

If the character you need is not at the start of the string, combine CODE with MID. For example, =CODE(MID(A1,3,1)) checks the third character instead of the first.

Conclusion Recap

CODE is mainly a diagnosis function. This lesson showed that it helps when a text value looks fine but the first character is actually something different, like a hidden line break, a leading space, or the wrong letter case.

The key thing to remember is that CODE only checks the first character unless you combine it with something like MID. Once you know the character code causing the problem, it becomes much easier to clean or replace it with CHAR, SUBSTITUTE, or another text function.

  • Summary: CODE returns the code for the first character in a text string.
  • Syntax: =CODE(text).
  • Key point: CODE checks only the first character unless you pair it with another function like MID.
  • Practical usage: Hidden-character checks, case checks, and text diagnostics.
  • Best pattern: Use CODE to identify a problem character, then use CHAR and SUBSTITUTE to clean it.
Tactical Arena
Share CODE Function!

Tell your friends about this post

Discussion

ExcelClash is an interactive platform designed to level up your Excel skills through real-world exercises and challenges. Sharpen your logic, solve real spreadsheet problems, and learn faster.

© 2026 ExcelClash, Inc. All rights reserved.