Can I assign an ASCII character to a VBA constant?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,650
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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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
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

Forum statistics

Threads
1,221,561
Messages
6,160,495
Members
451,653
Latest member
agata

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