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.
Table of Contents
1. Introduction
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.
Windows | ANSI |
Macintosh | Macintosh character set |
2. 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)
3. Arguments
text | Required. The character for which you want the corresponding code. |
4. 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.
5. How to convert a value to ANSI numbers
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)&"!"
6. List all characters based on ANSI numbers
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 |
7. 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.
7.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
7.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference B3 converted to hard-coded value using the F9 key. The CODE function requires non-empty values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
7.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
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