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.
Master the fundamentals of Excel CODE function
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.
Returns code for first character only
Returns numeric codes 1-255
Different codes for uppercase and lowercase
Inverse of CHAR function
Function-specific parameters
Function-specific return type
Analyze and categorize characters by code
Create custom sorting based on character codes
Check character types and ranges
Work with character encoding
Exact matching required
Returns numeric position
Handles missing text gracefully
=CODE(text)The text string for which you want the code of the first character
Numeric code (1-255) of the first character
Description: Returns the ASCII/ANSI code for the first character in a text string
Get ASCII code of character
Returns ASCII code 65 for uppercase letter 'A'
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 SubAnalyze 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 FunctionAnalyze first character of text strings
Detect if first character is uppercase
Check if character is in specific range
Create custom sorting by character codes
CODE with empty cell returns #VALUE!
=IF(A1="", "", CODE(A1))Solution: Check for empty cells: =IF(A1="", "", CODE(A1)) or use IFERROR.
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))
CODE only works with ASCII/ANSI (1-255)
=UNICODE(A1)Solution: Use UNICODE function for Unicode characters. CODE works with single-byte characters only.
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.