Defining Unicode characters as VBA global constants

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I need to make use of two Unicode special characters, the up arrow (&H2191) and the down arrow (&H2193).

Originally, I was using them in text string expressions like ChrW(CLng("&H2191")). But now I need to replicate that in multiple UDF. I'd prefer not to duplicate that code multiple times. If I ever change the characters, I will have to find all of the places and change each one.

So I tried to define global constants, but it isn't working. I first tried

Public Const gMaxCode as String = ChrW(CLng("&H2191"))

But it complained about it not being a "constant expression". It seemed like a constant expression to me. I don't know what part is not constant. I then tried

Public Const gMaxCode as String = ChrW(9593)

But, of course, that got the same error.

Is there a way I can get the up and down arrow characters into a global constant?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about
VBA Code:
Public Const gMaxCode As String = "&H2191"
and then convert it in the UDF
 
Upvote 0
You could also use "8593" for the up arrow & "8595" for the down arrow & then you can use ChrW(gMaxCode)
Rather than converting the Hex number to long first
 
Upvote 0
You could also use "8593" for the up arrow & "8595" for the down arrow & then you can use ChrW(gMaxCode)
Rather than converting the Hex number to long first
I thought of that, but I prefer to have the hex number in the constant as that's what is in the Unicode table. I already get things mixed up enough. :)
 
Upvote 0
Fair enough & thanks for the feedback
 
Upvote 0
Perfect. I should have thought of that. Thank you. :)


ChrW expects a Long not a String.
so you can just declare the const as Long :
VBA Code:
Public Const gMaxCode As Long = &H2191

Therefore, you don't have to take the extra step of converting it later on in code :
Code:
Dim sUpArrow As String

sUpArrow = ChrW(gMaxCode)
 
Upvote 0
Solution
ChrW expects a Long not a String.
Did anyone say it expects a string?
so you can just declare the const as Long :
VBA Code:
Public Const gMaxCode As Long = &H2191

Therefore, you don't have to take the extra step of converting it later on in code :
Code:
Dim sUpArrow As String

sUpArrow = ChrW(gMaxCode)
That does save a step. My code is now:

Code:
Public Const gMaxCode As Long = &H2191
   . . .
OverUnder = ChrW(gMaxCode) & Round(pReading - pGood, 0)

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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