Function that shows the Unicode code of a character

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Does this help?
Excel Workbook
AB
1a97
297a
1
Excel 2000
Cell Formulas
RangeFormula
B1=CODE(A1)
B2=CHAR(A2)


..I think that is ascii code though, not what you are looking for?
 
Upvote 0
Does such function exist in Excel?

Hi Jubinell

No. But you can use a simple udf to get it:

Code:
Function CodeUni(s As String, Optional bHex As Boolean = True) As Long
CodeUni = IIf(bHex, Hex(AscW(Left(s, 1))), AscW(Left(s, 1)))
End Function

Assuming the character in A1, use the udf in a cell as:

=CodeUni(a1)

to get the hexadecimal value of the character (the usual value in unicode maps)

or

=CodeUni(a1,False)

to get the decimal value of the character.

If the first parameter of the udf is a string you get the code of its first character.
 
Last edited:
Upvote 0
Sorry, remove the "As Long" from the procedure declaration:

Code:
Function CodeUni(s As String, Optional bHex As Boolean = True)
CodeUni = IIf(bHex, Hex(AscW(Left(s, 1))), AscW(Left(s, 1)))
End Function
 
Upvote 0
@pgc01

This seems to work flawlessly with one exception. Results starting with 0s were truncated in the output cell. For example the character 'A' returns 41 but its code should have been 0041.

I tried to set the format of the cells as text before using the functions but with that the functions wouldn't evaluate at all.

Any thoughts?
 
Upvote 0
Notice that the "A" character number is 41 hexadecimal or 65 decimal. In maps you get 41 or 0041 or U+0041 or 0x41, but these are the same number with different formats, as you know left zeros in a number have no value.

To get the hexadecimal numbers in the format 0000, try:

Code:
Function CodeUni(s As String, Optional bHex As Boolean = True)
If bHex Then
    CodeUni = Right("0000" & Hex(AscW(Left(s, 1))), 4)
Else
    CodeUni = AscW(Left(s, 1))
End If
End Function
 
Upvote 0
I'll add also the inverse function so that this thread can be used as reference.

Get the unicode character Γ (capital gamma) given its code we can use:

=CharUni(393)
=CharUni("393")
=CharUni("0393")
=CharUni(915,False)

The second parameter has the value False if we give the code as decimal, True or omitted if the code is hexadecimal.

Notice that if the hexadecimal code has letters the code must be entered as string or you may have a bad surprise.

Ex:

=CharUni(A93)
=CharUni("A93")

The first is the char corresponding to the value in A93, the second the char corresponding to the hexadecimal code "A93"

Code:
Function CharUni(sCode As String, Optional bHex As Boolean = True) As String
If bHex Then CharUni = ChrW("&H" & sCode) Else CharUni = ChrW(sCode)
End Function
 
Upvote 0
Hi PGC,

Sorry to open up an old post but I've encountered a problem with Unicode characters again.

I'm looking at the character
<table style="border-collapse: collapse; width: 48pt;" border="0" cellpadding="0" cellspacing="0" width="64"><col style="width: 48pt;" width="64"><tbody><tr style="height: 15pt;" height="20"> <td class="xl68" style="height: 15pt; width: 48pt;" height="20" width="64">�</td></tr></tbody></table>(that's an image)
in text, it is
&#55360;&#56738;

Now when i run CodeUni(), I get D840.
Yet when I run CharUni on D840, I get
<table style="border-collapse: collapse; width: 50pt;" border="0" cellpadding="0" cellspacing="0" width="67"><col style="width: 50pt;" width="67"><tbody><tr style="height: 15pt;" height="20"> <td style="height: 15pt; width: 50pt;" height="20" width="67">�</td> </tr></tbody></table>that's an image
in text, it is:


Any idea this is? This is a very rare sub-type of the Japanese radical hito (person). This character usually have 2 forms that are more common:, 人 and 亻. I do not have problems with these two forms.

Thank you once again for any insight you can give.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top