Unicode string literals in VBA

后端 未结 3 1162
萌比男神i
萌比男神i 2020-12-06 19:14

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

3条回答
  •  佛祖请我去吃肉
    2020-12-06 19:23

    Another approach is to use an Enum in combination with a function to provide VBA autocomplete based on friendly names. I prefer this method because it keeps all the Unicode definitions in one place, and uses the readable names throughout the rest of my project.

    ' List friendly names of Unicode characters
    Public Enum eUnicodeConst
        RightArrow
        LeftArrow
        Clock2
    End Enum
    
    '---------------------------------------------------------------------------------------
    ' Procedure : UniConst
    ' Author    : Adam Waller
    ' Date      : 7/7/2020
    ' Purpose   : Search for characters: https://emojipedia.org/
    '           : Look up UTF-16 Decimal value(s) from the following site:
    '           : http://www.fileformat.info/info/unicode/char/search.htm
    '---------------------------------------------------------------------------------------
    '
    Public Function UniConst(Text As eUnicodeConst) As String
        Select Case Text
            Case LeftArrow:     UniConst = ChrW(8592)
            Case RightArrow:    UniConst = ChrW(8594)
            Case Clock2:        UniConst = ChrW(55357) & ChrW(56657)
        End Select
    End Function
    

    Now in my code, I can just use the UniConst function anytime I need a Unicode character or snippet without having to look character codes.

提交回复
热议问题