excel - VBA 中的 Unicode 字符串文字

标签 excel vba string unicode-literals

我想(在 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/

相关文章:

javascript - 数组映射到带有换行符的字符串 - React

excel - 两个字典相交

excel - VBA 中的自动筛选

python - 在字典键中组合子字符串的方法

python - Python xlwt 库中的 set_style 不适用于包含文本的单元格

使用 VBA 导入文本文件时 Excel 内存不足警告

c++ - 为什么 new 能够创建一个字符串数组?

vba - 获取数据透视表的源范围

c++ - 适用于 Mac 的 Excel SDK

python - openpyxl:ValueError:值必须是 {'selection'、 'data'、 'field' 之一