CODE

Text Functions
(4.6/5)

Returns a numeric code for the first character in a text string. Returns the ASCII/ANSI code for the character. Essential for character analysis, sorting, and understanding character encoding in Excel.

Interactive Formula Tester

=CODE("A")

Complete Theory & Understanding

Master the fundamentals of Excel CODE function

Core Concept

The CODE function returns the ASCII/ANSI character code (1-255) for the first character in a text string. It's the inverse of CHAR function and is essential for character analysis, sorting, understanding character encoding, and creating character-based logic in Excel.

Why Use CODE?

  • Analyze and categorize characters by code
  • Create custom sorting based on character codes
  • Check character types and ranges
  • Work with character encoding

Key Characteristics

First Character Only

Returns code for first character only

CODE("ABC") returns 65 (for "A")

ASCII/ANSI Codes

Returns numeric codes 1-255

CODE("A") returns 65

Case-Sensitive

Different codes for uppercase and lowercase

CODE("A")=65, CODE("a")=97

CHAR Inverse

Inverse of CHAR function

CODE(CHAR(65)) returns 65

Function Anatomy

=CODE(parameters...)
Required
Parameters:

Function-specific parameters

Returns
Return Value:

Function-specific return type

Primary Use Cases

Character Analysis

Analyze and categorize characters by code

Sorting Logic

Create custom sorting based on character codes

Character Validation

Check character types and ranges

Encoding Work

Work with character encoding

Theory Summary

Precise

Exact matching required

Position-Based

Returns numeric position

Error-Safe

Handles missing text gracefully

Syntax & Parameters

=CODE(text)
Required
text:

The text string for which you want the code of the first character

Returns
Return Value:

Numeric code (1-255) of the first character

Description: Returns the ASCII/ANSI code for the first character in a text string

Interactive Examples

Basic Character Code

Get ASCII code of character

""A""
=CODE("A")
65

Returns ASCII code 65 for uppercase letter 'A'

VBA Implementation & Automation

Basic CODE in VBA

Simple VBA implementation of CODE function

' Basic CODE in VBA
Range("B1").Value = Application.WorksheetFunction.Code(Range("A1").Value)

' Using VBA Asc function (equivalent)
Dim text As String
text = "A"
Dim charCode As Integer
charCode = Asc(text)
Debug.Print charCode ' Output: 65

' Get codes for multiple characters
Sub GetCharacterCodes()
    Dim cell As Range
    For Each cell In Range("A1:A10")
        If Not IsEmpty(cell.Value) Then
            Dim firstChar As String
            firstChar = Left(cell.Value, 1)
            cell.Offset(0, 1).Value = Asc(firstChar)
        End If
    Next cell
End Sub

' Compare character codes
Sub CompareCharacters()
    Dim upperCode As Integer
    Dim lowerCode As Integer
    upperCode = Asc("A")
    lowerCode = Asc("a")
    Debug.Print "A = " & upperCode & ", a = " & lowerCode
    ' Output: A = 65, a = 97
End Sub

Advanced Character Analysis

Analyze characters and categorize by code ranges

' Function to categorize character by code
Function CategorizeCharacter(text As String) As String
    Dim code As Integer
    code = Asc(Left(text, 1))
    
    If code >= 48 And code <= 57 Then
        CategorizeCharacter = "Number"
    ElseIf code >= 65 And code <= 90 Then
        CategorizeCharacter = "Uppercase"
    ElseIf code >= 97 And code <= 122 Then
        CategorizeCharacter = "Lowercase"
    ElseIf code >= 32 And code <= 47 Or code >= 58 And code <= 64 Then
        CategorizeCharacter = "Punctuation"
    Else
        CategorizeCharacter = "Other"
    End If
End Function

' Check if character is uppercase
Function IsUpperCase(text As String) As Boolean
    Dim code As Integer
    code = Asc(Left(text, 1))
    IsUpperCase = (code >= 65 And code <= 90)
End Function

' Get all character codes in string
Function GetAllCodes(text As String) As String
    Dim i As Integer
    Dim result As String
    result = ""
    For i = 1 To Len(text)
        If i > 1 Then result = result & ", "
        result = result & Asc(Mid(text, i, 1))
    Next i
    GetAllCodes = result
End Function

Business Applications

Character Analysis

Analyze first character of text strings

=CODE(A1)

Case Detection

Detect if first character is uppercase

=IF(AND(CODE(A1)>=65, CODE(A1)<=90), "Uppercase", "Other")

Character Validation

Check if character is in specific range

=IF(CODE(A1)>=48, IF(CODE(A1)<=57, "Number", "Not Number"), "Not Number")

Sorting Logic

Create custom sorting by character codes

=CODE(A1)

Common Issues & Solutions

Empty Cell

CODE with empty cell returns #VALUE!

=IF(A1="", "", CODE(A1))

Solution: Check for empty cells: =IF(A1="", "", CODE(A1)) or use IFERROR.

Multiple Characters

CODE only returns code for first character

=CODE(MID(A1, 2, 1))

Solution: This is expected behavior. Use MID with CODE to get codes for other positions: =CODE(MID(A1, 2, 1))

Unicode Characters

CODE only works with ASCII/ANSI (1-255)

=UNICODE(A1)

Solution: Use UNICODE function for Unicode characters. CODE works with single-byte characters only.

Case Sensitivity

CODE returns different values for uppercase/lowercase

=CODE(UPPER(A1))

Solution: This is expected. Uppercase A=65, lowercase a=97. Use UPPER or LOWER to normalize if needed.

Performance Tips & Best Practices

⚡ Performance Optimization

  • Use CODE efficiently for character analysis
  • Combine CODE with IF for character validation
  • Cache CODE results when used multiple times
  • Consider UNICODE for extended character sets

🎯 Best Practices

  • Remember CODE only returns code for first character
  • Use CODE to check character ranges (48-57 for digits, 65-90 for uppercase)
  • Combine with MID to get codes for other positions
  • Document CODE usage for character validation logic

💡 Pro Tips

  • CODE(48-57) = digits 0-9, CODE(65-90) = A-Z, CODE(97-122) = a-z
  • CODE is case-sensitive: CODE("A")=65, CODE("a")=97
  • Use CODE with CHAR: CODE(CHAR(65)) returns 65
  • Use UNICODE for characters beyond ASCII range