Syntax. CODE(text)
Definition. This function returns the code of the first character in a string. The code corresponds to the character set used by your computer.
Arguments
text (required). The text from which the code for the first character is returned
Background. Sometimes it is useful to know the code of a character, such as a special character. CODE() is the counterpart of CHAR(), which returns the character for a code value.
Because the function returns the code value for the first character of the text, you need to use only a single-letter argument. CODE("Excel")
, CODE("Ergonomics")
and CODE("E")
all return 69
for the capital letter E.
To return the code for the second or third character, use the MID() function. The formula
=CODE(MID("Excel",2,1))
returns the code for the lowercase x: 120
.
You can use this function to assign category numbers to the first names in a column. Assume that all first names beginning with A have the category number 1, all first names beginning with B have the number 2, and so on. Uppercase and lowercase characters should be treated the same. The formula uses another text function: UPPER().
=CODE(UPPER("torsten"))-64
The formula returns the value 20
(see Figure 8-1). This is the position of T in the alphabet. The UPPER() function changed the argument torsten to TORSTEN. Because the uppercase A has the code 65, you have to subtract 64 to get the correct position in the alphabet.