How to use the CODE function
What is the CODE function?
The CODE function returns a numeric code for the first character of the text argument, determined by your computer's character set.
What is my computers character set?
A character set is a collection of characters that can be represented by a computer. Different operating systems use different character sets, such as ANSI for Windows and Macintosh for Mac OS.
What is the difference between ANSI and ASCII?
ANSI stands for American National Standards Institute and ASCII stands for American Standard Code for Information Interchange. ANSI and ASCII are two different character encoding standards, the difference between them is that ANSI uses 8 bits per character, while ASCII uses 7 bits per character. This means that ANSI can support up to 256 different characters and ASCII can only support up to 128 different characters.
How can 7 bits represent 128 characters?
7 bits means 7 binary number or positions, the binary system has two possible values 0 (zero) and 1. 27 = 128 possible combinations, each can be assigned to a different character. Every character in the ASCII encoding standard has a unique 7-bit representation.
How can 8 bits represent 256 characters?
8 bits means 8 binary number or positions, the binary system has two possible numbers 0 (zero) and 1. 28 = 256 possible combinations, each can be assigned to a different character. Every character in the ANSI encoding has a unique 8-bit representation.
How many different characters can the CODE function handle?
The ANSI range in windows is between 0 to 255 so 256 different characters, however, the CODE function only converts the first character in a value. The remaining characters are left out.
If you want to get the code for a character that is not in the ANSI range (0-255), you can use the UNICODE function instead.
What is Unicode?
Both ANSI and ASCII encodings have been replaced by Unicode that defines a universal character set including almost all characters in the world. Excel has two functions if you want to work with Unicode characters: UNICODE function and UNICHAR function.
Table of Contents
Windows | ANSI |
Macintosh | Macintosh character set |
1. CODE Function Syntax
The CODE function has only one argument. If the text argument is longer than one character then only the first character is converted in to ANSI code.
CODE(text)
2. CODE Function Arguments
text | Required. The character for which you want the corresponding code. |
3. CODE Function example
The image above demonstrates the CODE function in cell D3, the CODE function argument is a cell reference pointing to cell B3.
Formula in cell D3:
Cell B3 contains only one character which is the A. The corresponding code for letter A is 65 which is displayed in cell D3.
3.1 Explaining formula
CODE(B3)
becomes
CODE("A")
and returns 65.
A to Z corresponds to numbers 65 to 90. a to z corresponds to numbers 97 to 122.
4. How to convert a value to ANSI numbers - CODE Function
This example shows how to convert each character in a value to ANSI code using the CODE function in Excel. Cell B3 contains "Hello!" without the double quotes.
The following formula is useful if you want to see hidden characters or compare two different values that seems to be the same but are not.
Formula in cell D3:
The formula splits the text string in to an array of characters, each container has one character. The formula then converts the characters to ANSI code and finally joins them using a single delimiting character, in this example , (comma).
Cell D3 displays the result, string "Hello!" has these ANSI code values: 72, 101, 108, 108, 111, and 33.
Explaining formula
Step 1 - Count characters in cell
The LEN function returns the number of characters in a cell value.
LEN(value)
LEN(B3)
becomes
LEN("Hello!")
and returns 6. There are six characters in cell B3.
Step 2 - Create numbers from 1 to n
The SEQUENCE function creates a list of sequential numbers.
SEQUENCE(rows, [columns], [start], [step])
SEQUENCE(,LEN(B3))
becomes
SEQUENCE(,6)
and returns {1, 2, 3, 4, 5, 6}.
Step 3 - Split characters in cell
The MID function returns a substring from a string based on the starting position and the number of characters you want to extract.
MID(text, start_num, num_chars)
MID(B3,SEQUENCE(,LEN(B3)),1)
becomes
MID("Hello!",{1, 2, 3, 4, 5, 6},1)
and returns {"H","e","l","l","o","!"}.
Step 4 - Convert characters to numbers
CODE(MID(B3,SEQUENCE(,LEN(B3)),1))
becomes
CODE({"H","e","l","l","o","!"})
and returns {72,101,108,108,111,33}.
Step 5 - Join strings
The TEXTJOIN function allows you to combine text strings from multiple cell ranges and also use delimiting characters.
TEXTJOIN(delimiter, ignore_empty, text1, [text2], ...)
TEXTJOIN(",",TRUE,CODE(MID(B3,SEQUENCE(,LEN(B3)),1)))
becomes
TEXTJOIN(",",TRUE,{72,101,108,108,111,33})
and returns
"72,101,108,108,111,33".
This example shows two values in cells B3 and B6, they seem identical but are not. The value in cell B6 has a new line character between "o" and "!", you can create this character in a cell by pressing Alt + Enter, however, to use it in a formula you ned the CHAR functtion.
For example, to buildd the value in cell B6 with a formula you can do like this:
="Hello"&CHAR(10)&"!"
5. List all characters based on ANSI numbers - CODE Function
The image above shows upper and lower letters in the ANSI encoding set and their corresponding number, here is the formula in cell B2:
The formula spills the values to cells below and to the right as far as needed automatically in Excel 365.
Explaining formula
Step 1 - Create numbers from 65 to 90
The SEQUENCE function creates a list of sequential numbers.
SEQUENCE(rows, [columns], [start], [step])
SEQUENCE(26, , 65)
returns {65;66;67; ... ; 90}
Step 2 - Create characters
The CHAR function converts a number to the corresponding ANSI character determined by your computers character set.
CHAR(number)
CHAR(SEQUENCE(26, , 65))
becomes
CHAR({65;66;67; ... ; 90})
and returns {"A";"B";"C"; ... ; "Z"}
Step 3 - Join arrays horizontally
The HSTACK function lets you combine cell ranges or arrays, it joins data to the first blank cell to the right of a cell range or array (horizontal stacking)
HSTACK(array1,[array2],...)
HSTACK(CHAR(SEQUENCE(26, , 65)), SEQUENCE(26, , 65), CHAR(SEQUENCE(26, , 97)), SEQUENCE(26, , 97))
becomes
HSTACK({"A";"B";"C"; ... ; "Z"}, {65;66;67; ... ; 90}, {"a";"b";"c"; ... ; "z"}, {97;98;99; ... ; 122})
and returns
{"A",65,"a",97;"B", ... , "z",122}
Step 4 - Add column headers
The VSTACK function lets you combine cell ranges or arrays, it joins data to the first blank cell at the bottom of a cell range or array.
VSTACK(array1,[array2],...)
VSTACK({"Upper", "Code", "Lower", "Code"}, HSTACK(CHAR(SEQUENCE(26, , 65)), SEQUENCE(26, , 65), CHAR(SEQUENCE(26, , 97)), SEQUENCE(26, , 97)))
becomes
VSTACK({"Upper", "Code", "Lower", "Code"}, {"A",65,"a",97;"B", ... , "z",122})
and returns
"Upper","Code","Lower","Code";"A",65, ... ,"z",122}.
The table below shows all characters in the ANSI encoding set.
1 | 33 | ! | 65 | A | 97 | 129 | | 161 | 193 | Á | 225 | á | |
2 | 34 | " | 66 | B | 98 | 130 | ‚ | 162 | 194 | Â | 226 | â | |
3 | 35 | # | 67 | C | 99 | 131 | ƒ | 163 | 195 | Ã | 227 | ã | |
4 | 36 | $ | 68 | D | 100 | 132 | „ | 164 | 196 | Ä | 228 | ä | |
5 | 37 | % | 69 | E | 101 | 133 | … | 165 | 197 | Å | 229 | å | |
6 | 38 | & | 70 | F | 102 | 134 | † | 166 | 198 | Æ | 230 | æ | |
7 | 39 | ' | 71 | G | 103 | 135 | ‡ | 167 | 199 | Ç | 231 | ç | |
8 | 40 | ( | 72 | H | 104 | 136 | ˆ | 168 | 200 | È | 232 | è | |
9 | 41 | ) | 73 | I | 105 | 137 | ‰ | 169 | 201 | É | 233 | é | |
10 | 42 | * | 74 | J | 106 | 138 | Š | 170 | 202 | Ê | 234 | ê | |
11 | 43 | + | 75 | K | 107 | 139 | ‹ | 171 | 203 | Ë | 235 | ë | |
12 | 44 | , | 76 | L | 108 | 140 | Œ | 172 | 204 | Ì | 236 | ì | |
13 | 45 | - | 77 | M | 109 | 141 | | 173 | 205 | Í | 237 | í | |
14 | 46 | . | 78 | N | 110 | 142 | Ž | 174 | 206 | Î | 238 | î | |
15 | 47 | / | 79 | O | 111 | 143 | | 175 | 207 | Ï | 239 | ï | |
16 | 48 | 0 | 80 | P | 112 | 144 | | 176 | 208 | Ð | 240 | ð | |
17 | 49 | 1 | 81 | Q | 113 | 145 | ‘ | 177 | 209 | Ñ | 241 | ñ | |
18 | 50 | 2 | 82 | R | 114 | 146 | ’ | 178 | 210 | Ò | 242 | ò | |
19 | 51 | 3 | 83 | S | 115 | 147 | “ | 179 | 211 | Ó | 243 | ó | |
20 | 52 | 4 | 84 | T | 116 | 148 | ” | 180 | 212 | Ô | 244 | ô | |
21 | 53 | 5 | 85 | U | 117 | 149 | • | 181 | 213 | Õ | 245 | õ | |
22 | 54 | 6 | 86 | V | 118 | 150 | – | 182 | 214 | Ö | 246 | ö | |
23 | 55 | 7 | 87 | W | 119 | 151 | — | 183 | 215 | × | 247 | ÷ | |
24 | 56 | 8 | 88 | X | 120 | 152 | ˜ | 184 | 216 | Ø | 248 | ø | |
25 | 57 | 9 | 89 | Y | 121 | 153 | ™ | 185 | 217 | Ù | 249 | ù | |
26 | 58 | : | 90 | Z | 122 | 154 | š | 186 | 218 | Ú | 250 | ú | |
27 | 59 | ; | 91 | [ | 123 | 155 | › | 187 | 219 | Û | 251 | û | |
28 | 60 | < | 92 | \ | 124 | 156 | œ | 188 | 220 | Ü | 252 | ü | |
29 | 61 | = | 93 | ] | 125 | 157 | | 189 | 221 | Ý | 253 | ý | |
30 | 62 | > | 94 | ^ | 126 | 158 | ž | 190 | 222 | Þ | 254 | þ | |
31 | 63 | ? | 95 | _ | 127 | 159 | Ÿ | 191 | 223 | ß | 255 | ÿ | |
32 | 64 | @ | 96 | ` | 128 | 160 | 192 | 224 | à | 256 |
6. CODE function returns #VALUE! error
The CODE function returns a #VALUE! error if the cell is empty.
Formula in cell D3:
Explaining formula
CODE(B3)
becomes
CODE("")
and returns #VALUE! error.
Useful resources
CODE function - Microsoft support
Excel CODE and CHAR Function Examples
'CODE' function examples
In this blog post I will demonstrate methods on how to find, select, and deleting blank cells and errors. Why […]
This article explains different techniques that filter rows/records that contain a given text string in any of the cell values […]
What's on this page Reverse text Insert random characters Convert letters to numbers How to shuffle characters in the alphabet […]
Functions in 'Text' category
The CODE function function is one of 29 functions in the 'Text' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form