我想(在 VBA 类模块中)声明一些包含日语字符的私有(private)常量字符串。有没有一种方法可以构造 String
文字(或以某种方式组合文字),这些文字可以被接受为 Const
声明中的初始值设定项?即类似:
Private Const MY_CONST = ...
或
Private Const MY_CONST As String = ...
我使用 MS Excel v14.0.6112.5000 (MS Office Professional Plus 2010)。
什么不起作用:
- 将日语字符直接粘贴到字符串文字中(例如
... = "変数"
),因为 VBA 编辑器会弄乱这些字符; - 使用
ChrW()
或ChrW$()
(例如... = ChrW$(22793) & ChrW$(25968)
),因为Const
初始化器中不允许函数调用。
我不喜欢什么:
- 通过创建返回字符串的
Private Property Get
来伪造Const
,因为每次我访问该属性时都会重新创建该字符串(另外,这很困惑且丑陋...... .但是,好吧,最后两件事只是一个品味问题)。
最佳答案
Faking the Const by creating Private 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).
您无需在每次访问该属性时重新创建该字符串。
虽然从口味上来说这仍然很难看,但创建一个只读属性(本质上是 Const
,因为它没有 Property Let
过程),并在 Class_Initialize
事件中构造字符串:
'## CLASS MODULE
Private pUnicodeString As String
Sub Class_Initialize()
pUnicodeString = ChrW(22793) & ChrW(25968)
End Sub
Property Get UnicodeString() As String
UnicodeString = pUnicodeString
End Property
然后像这样调用它:
'## STANDARD MODULE
Sub Test()
Dim c As myClass
Set c = New myClass
[A1].Value = c.UnicodeString
End Sub
关于excel - VBA 中的 Unicode 字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23678033/