CODE function

The CODE function returns the numeric Unicode (ASCII) value of the first character in a given text string, useful for character-level analysis in spreadsheets.

=CODE(text)

Generate a CODE formula

Describe what you need. The generator will reach for CODE where CODE is the right tool, and tell you when it is not.

How to get a better answer
  • Name your columns by letter and by header: "column F (Net Value)" beats "the amount column".
  • State every condition, including the negatives — "not cancelled" changes the formula's shape.
  • Say where the data starts if it is not row 1, and whether it will grow.
  • Check the settings above match your spreadsheet: the wrong argument separator is a syntax error on your machine.

Arguments

How CODE reads its arguments
textrequiredCODE
ArgumentRequiredDescription
textRequiredA required text argument; if omitted or empty, CODE returns #VALUE!; non-text values are coerced to text before evaluation.

Returns

A single numeric value representing the Unicode code point of the first character.

Availability

Excel: All · Google Sheets: Supported

Worked examples

1. Get the ASCII code of a vehicle name's first letter

VehicleOdometerService DateCostGarage
Truck011200002023-05-10450North Garage
Van12800002023-06-15300East Garage
Car07500002023-07-20200South Garage
=CODE(A2)

Result: 84

Cell A2 contains the text "Truck01". The first character is "T", whose Unicode (ASCII) code is 84. CODE reads only that initial character, ignores the rest of the string, and returns 84.

2. Find the code for a garage's initial letter

VehicleOdometerService DateCostGarage
Truck011200002023-05-10450North Garage
Van12800002023-06-15300East Garage
Car07500002023-07-20200South Garage
=CODE(E3)

Result: 69

E3 holds "East Garage". The leading character "E" maps to the Unicode value 69. CODE extracts that character and returns 69, which can be useful for sorting or categorising garages alphabetically.

3. Extract the code of the month abbreviation from a service date

VehicleOdometerService DateCostGarage
Truck011200002023-05-10450North Garage
Van12800002023-06-15300East Garage
Car07500002023-07-20200South Garage
=CODE(TEXT(C4,"mmm"))

Result: 74

C4 contains the date "2023-07-20". TEXT(C4,"mmm") converts it to the three-letter month abbreviation "Jul". CODE then reads the first character "J", whose Unicode value is 74, and returns that number.

Common errors

Which CODE error are you seeing?
CODE returned an error#VALUE!
Wrap the reference in IF(LEN(text)>0, CODE(text), "") or ensure the cell contains at least one character.
#N/A
Use IFNA or IFERROR around the lookup to supply a fallback value before passing it to CODE.
#REF!
Correct the cell reference or replace the broken link with a valid cell address before calling CODE.
ErrorWhy it happensHow to fix it
#VALUE!The text argument is an empty string, so there is no first character for CODE to evaluate.Wrap the reference in IF(LEN(text)>0, CODE(text), "") or ensure the cell contains at least one character.
#N/AThe argument points to a cell that already contains the #N/A error (e.g., a failed lookup). CODE propagates that error.Use IFNA or IFERROR around the lookup to supply a fallback value before passing it to CODE.
#REF!The reference supplied to CODE is invalid because the referenced cell was deleted, producing a #REF! error.Correct the cell reference or replace the broken link with a valid cell address before calling CODE.

Tips and when to use something else

  • CODE works with the first character only; if you need the code for a later character, combine it with MID or RIGHT.
  • When you need the opposite conversion (code to character), use the CHAR function instead of CODE.
  • For Unicode characters beyond the basic ASCII range, Excel’s CODE still returns the correct value, but Google Sheets may require the UNICODE function (not listed here).
  • If you need the numeric value of an entire string, consider using SUMPRODUCT(CODE(MID(text,ROW(INDIRECT("1:"&LEN(text))),1))) to sum each character’s code.

Frequently asked questions

Why does CODE return a number instead of the character itself?
CODE is designed to give you the underlying numeric representation of a character, which is useful for comparisons, sorting, or creating custom encoding schemes. To get the character back, use CHAR with the number you obtained.
Can CODE handle Unicode characters like emojis?
In Excel, CODE returns the Unicode code point for any character, including emojis, as long as the font supports it. Google Sheets, however, only returns the first 255 code points; for full Unicode support use the UNICODE function.
What happens if I pass a number like 123 to CODE?
Excel coerces the number to the text string "123" and then returns the code for the first character "1" (which is 49). If you need the code for the numeric value itself, convert the number to text first with TEXT.
Is there a way to get the code for every character in a cell without writing multiple formulas?
Yes. In Excel you can use an array formula such as =CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)) and then press Ctrl+Shift+Enter. In Google Sheets, wrap the same expression with ARRAYFORMULA.

Need a different formula?

The full generator is not scoped to one function — describe any spreadsheet problem and it will pick.

Open the formula generator

Reviewed 2026-09-17