Can I assign an ASCII character to a VBA constant?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
Is there any way to assign various Unicode characters to string constants in VBA? This code works.

VBA Code:
Dim DownArrow As String
DownArrow = ChrW(&H2193)
Dim UpArrow As String
UpArrow = ChrW(&H2191)

But I was hoping for something like

Code:
Const DownArrow as String = &H2193
Const DownArrow as String = "2193"x

Thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I suppose you could. Though I don't know why.
VBA Code:
Const DownArrow As String = "&H2193"

You still would have to convert the Unicode string constant to the character.
VBA Code:
ChrW(DownArrow)
 
Upvote 0
Solution
I suppose you could. Though I don't know why.
VBA Code:
Const DownArrow As String = "&H2193"

You still would have to convert the Unicode string constant to the character.
VBA Code:
ChrW(DownArrow)
That's pretty much the same as the original solution

So is there no way to define a Const that contains the down arrow symbol like I can do with other symbols, like this:

VBA Code:
Const UpArrow As String = "^"
 
Upvote 0
You could define variables as public and initialize those values and call that macro first in your sub.
VBA Code:
Public downarrow As String
Public uparrow As String

Sub InitializeConstants()
downarrow = ChrW(8595)
uparrow = ChrW(8593)
End Sub

Sub test()
Call InitializeConstants
Range("A1") = downarrow
Range("A2") = uparrow
End Sub

They're not really constants but I used that for readability.
 
Upvote 0
You could define variables as public and initialize those values and call that macro first in your sub.
VBA Code:
Public downarrow As String
Public uparrow As String

Sub InitializeConstants()
downarrow = ChrW(8595)
uparrow = ChrW(8593)
End Sub

Sub test()
Call InitializeConstants
Range("A1") = downarrow
Range("A2") = uparrow
End Sub

They're not really constants but I used that for readability.
Yeah, they are not constants in the strict sense of the word, but that approach does allow me to define them once and use them multiple times.
 
Upvote 0
You could also use functions.
 
Upvote 0
I mean you'd have something like:

VBA Code:
Function DownArrow() as String
DownArrow = ChrW(&H2193)
end function

so you can just use DownArrow in your code wherever you need it.
 
Upvote 0
I mean you'd have something like:

VBA Code:
Function DownArrow() as String
DownArrow = ChrW(&H2193)
end function

so you can just use DownArrow in your code wherever you need it.

That gave me an idea. I already have a Constants module in my add-in module. I added this code:

VBA Code:
' Character constants that cannot be entered directly
' To use them, do something like =ChrW(charUpArrow)
Public Const charDnArrow As String = "&H2191"
Public Const charUpArrow As String = "&H2193"

Then in any other code, all I need to do is this:

Code:
Dim symLocal As String        'Use down arrow for local names
symLocal = ChrW(charDnArrow)     'From Constants module
Dim symGlobal As String       'Use up arrow for global names
symGlobal = ChrW(charUpArrow)    'From Constants module

Comments?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
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