VBA中的Unicode字符串文字 [英] Unicode string literals in VBA

查看:975
本文介绍了VBA中的Unicode字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在VBA类模块中声明一些包含日语字符的私有常量字符串.有没有一种方法可以构造String文字(或以某种方式组合文字),使其可以在Const声明中作为初始化程序接受?即:

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 = ...

Private Const MY_CONST As String = ...

我使用MS Excel v14.0.6112.5000(MS Office Professional Plus 2010).

I use MS Excel v14.0.6112.5000 (MS Office Professional Plus 2010).

什么无效:

  • 将日语字符直接用字符串文字(例如... = "変数")粘贴,因为VBA编辑器会弄乱字符;
  • 使用ChrW()ChrW$()(例如... = ChrW$(22793) & ChrW$(25968)),因为在Const初始化程序中不允许使用函数调用.
  • Pasting the Japanese chars directly in a string literal (e.g. ... = "変数") because the VBA editor will mess with the chars;
  • Using ChrW() or ChrW$() (e.g. ... = ChrW$(22793) & ChrW$(25968)), because function calls are not allowed in Const initializers.

什么我不想要:

What I wouldn't like:

  • 通过创建返回字符串的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).

推荐答案

通过创建私有属性来使常量失效返回字符串,因为每次访问属性时都会重新创建字符串(而且,这令人困惑又丑陋……但是,最后两件事只是一个问题口味).

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).

您无需在每次访问属性时重新创建字符串.

You need not recreate the string each time you access the property.

尽管这仍然很丑陋,但请设置一个只读属性(本质上为Const,因为它没有Property Let过程),然后在Class_Initialize事件中构造字符串:

While this is still ugly as a matter of taste, make a read-only property (essentially Const, since it doesn't have a Property Let procedure), and construct the string in the Class_Initialize event:

'## 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

这篇关于VBA中的Unicode字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆