I would like to declare (in a VBA class module) some private constant strings that contain Japanese characters. Is there a way to construct String
literals (or combining literals in a way) that may be accepted as initializers in a Const
declaration? i.e. something like:
Private Const MY_CONST = ...
or
Private Const MY_CONST As String = ...
I use MS Excel v14.0.6112.5000 (MS Office Professional Plus 2010).
What won't work:
- Pasting the Japanese chars directly in a string literal (e.g.
... = "変数"
) because the VBA editor will mess with the chars; - Using
ChrW()
orChrW$()
(e.g.... = ChrW$(22793) & ChrW$(25968)
), because function calls are not allowed inConst
initializers.
What I wouldn't like:
- Faking the
Const
by creatingPrivate Property Get
returning the string, because the string will be recreated every time I access the property (plus, is confusing and ugly... but, okay, the last two things are rather a matter of taste).
You need not recreate the string each time you access the property.
While this is still ugly as a matter of taste, make a read-only property (essentially
Const
, since it doesn't have aProperty Let
procedure), and construct the string in theClass_Initialize
event:And then invoke it like:
The encoding of VBA source file is Windows-1252, which does not support Japanese.
You cannot change the encoding of the source file, so you have to write its binary equivalent and then convert it before using it
Use notepad to convert the string: copy-paste the unicode string, save the file as unicode (not utf-8) and open it as ANSI, then copy-paste it into the VBA editor without the first two characters (ÿþ), which is the BOM marker
Explanation
変数 is
U+5909 U+6570
in unicode which is0x09 0x59 0x70 0x65
in UTF-16LE (Windows unicode encoding), and this sequence corresponds to<tab>Ype
in Windows-1252