Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

excel - Unicode string literals in VBA

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() or ChrW$() (e.g. ... = ChrW$(22793) & ChrW$(25968)), because function calls are not allowed in Const initializers.

What I wouldn't like:

  • 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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

And then invoke it like:

'## STANDARD MODULE
Sub Test()
Dim c As myClass
Set c = New myClass

[A1].Value = c.UnicodeString

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...