Returning unicode characters from a UDF

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
How can I return unicode special characters from a UDF? I am writing one that examines a range of cells and returns a value with the prefix indicating where that cell fits in a range. I wold like that prefix to be the up arrow (Unicode 2191) or the down arrow (unicode 2192).

I tried returning ChrW(2192) & Abs(Round(pReading - pMin, 0)).

The value in the watch window is "?9", but the value in the cell is "9[]", where "[]" represents a square.

How can I get it to return a string that will display as "^9", but with the up arrow in place of the "^".

If I replace the ChrW(2192) with "-" ("-" & Abs(Round(pReading - pMin, 0)), it correctly displays "-9".

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
ChrW requires a decimal value. Therefore, first convert your hex value into a decimal value, and then use ChrW to return the unicode character...

Hex constant -----------------------------------> &H2191

Convert to decimal value 8593 ----------------> CLng("&H2191")

Return unicode character ----------------------> ChrW(CLng("&H2191"))

So you would have the following...

VBA Code:
ChrW(CLng("&H2191")) & Abs(Round(pReading - pMin, 0))

Hope this helps!
 
Upvote 0
Solution
ChrW requires a decimal value. Therefore, first convert your hex value into a decimal value, and then use ChrW to return the unicode character...

Hex constant -----------------------------------> &H2191

Convert to decimal value 8593 ----------------> CLng("&H2191")

Return unicode character ----------------------> ChrW(CLng("&H2191"))

So you would have the following...

VBA Code:
ChrW(CLng("&H2191")) & Abs(Round(pReading - pMin, 0))

Hope this helps!
Wow! That works like a champ. Thanks.

I wonder why my version caused the square character to follow the digit ("9[]")?
 
Upvote 0
Wow! That works like a champ. Thanks.

You're very welcome!

I wonder why my version caused the square character to follow the digit ("9[]")?

With regards to ChrW(2192), you're supplying ChrW the decimal value 2192. This value is equal to the hex value &H0890 or the unicode code point U+0890. This code point, however, is unassigned, as per the following unicode character reference...


Therefore, since it's unassigned, ChrW returns a question mark. In my watch window window, it's displays as "?9", while in my cell it displays as "9?".
 
Upvote 0
Therefore, since it's unassigned, ChrW returns a question mark. In my watch window window, it's displays as "?9", while in my cell it displays as "9?".
OK, but why is the "?" after the "9" instead of before it? Why is it "9?" instead of "?9"? Just curious.
 
Upvote 0
That's a good question. Unfortunately, I don't know the answer. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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