CODE()
Returns the numeric character code for the first character of a text string.
CODE(text)CODE returns the number corresponding to the first character of text, based on the character set your computer uses. For standard characters this is the same numbering CHAR uses in reverse.
It's mainly used alongside CHAR to convert between characters and their numeric codes, or to test what kind of character — letter, digit, or symbol — starts a string.
Arguments
- text
- The text whose first character's code you want.
Examples
=CODE("A")The character code for a capital A.
=CODE(LEFT(A2,1))Checks the code of the first character of A2 to test if a value starts with a digit.
CODE tricks, tips, and workbook hacks
CODE is easier to remember when you learn the small patterns around it: where it fits, what can break it, and how to combine it with neighboring functions.
Use these practical notes as a working checklist before putting the formula into a real report, model, dashboard, tracker, or reusable template.
Quick wins before you trust the result
- Start with the simplest CODE formula that answers the question, then add conditions or error handling only when needed.
- Keep input ranges consistent in size, shape, and meaning.
- Test the formula on one row or one small sample before filling it down a large workbook.
- Add a note beside important formulas explaining what business question they answer.
- Pair the formula with related checks so mistakes are easier to spot.
| Trick or hack | Formula pattern | Best use | Watch out |
|---|---|---|---|
| Build the formula in layers | CODE(text) | When the full formula feels hard to read or debug. | Confirm each argument or nested function before combining everything. |
| Use structured references | =CODE(Table1[Column]) | Excel Tables make formulas easier to read and expand automatically. | Rename table columns carefully so formulas remain clear. |
| Protect against bad input | =IFERROR(CODE(...),"Check input") | Dashboards and shared templates where users need a readable message. | Do not hide errors until you understand why they happen. |
| Document the expected result | Add a note or nearby label | Reusable workbooks, finance models, templates, and handoff files. | A clever formula without context becomes expensive to maintain. |
| Mistake | What it looks like | Fix |
|---|---|---|
| Using a wider range than intended | The result changes unexpectedly after new rows or helper columns are added. | Use Excel Tables or named ranges to make the intended input explicit. |
| Mixing text, numbers, dates, and blanks | The answer technically works but does not match the business meaning. | Clean and validate the source column before applying the function. |
| Copying a formula with moving references | The first row works; later rows drift into the wrong range. | Use absolute references, structured references, or named ranges where needed. |
| Scenario | Use | Avoid |
|---|---|---|
| You need a quick answer from clean data | CODE with the smallest clear range | Overbuilding the formula with unnecessary wrappers |
| The result will appear in a dashboard | Add checks, labels, and readable fallbacks | Leaving raw errors or unexplained blanks for viewers |
| The workbook will grow over time | Excel Tables, named ranges, or structured references | Hard-coded ranges that miss new rows |
| The data comes from another system | Clean inputs before summarizing or transforming | Assuming pasted data has correct types and spacing |
Practice drills
- Write CODE once with direct cell references and once with an Excel Table reference.
- Break the formula on purpose, then use the error message to identify which argument failed.
- Create a small before/after example that explains the formula to another learner.
