vba 在excel中的vba中声明一个unicode字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7269399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:49:57  来源:igfitidea点击:

declaring a unicode string in vba in excel

excel-vbaunicodeencodingvbaexcel

提问by Stavros

I am trying to create a substitute() that will convert greek characters to latin.

我正在尝试创建一个将希腊字符转换为拉丁字符的替代()。

The problem is that after declaring

问题是声明后

Dim Source As String
Source = "αβγδεζηικλμνξοπρστθφω"  

Source is interpreted as "áa?????éê?ìí??e?ó???ù"
is there any way use unicode at declaration level?

来源被解释为“áa?????éê?ìí??e?ó???ù”
有没有办法在声明级别使用unicode?

采纳答案by JMax

You can try StrConv:

你可以试试StrConv

StrConv("αβγδεζηικλμνξοπρστθφω", vbUnicode)

Source : http://www.techonthenet.com/excel/formulas/strconv.php

来源:http: //www.techonthenet.com/excel/formulas/strconv.php

[EDIT] Another solution:

[编辑] 另一个解决方案:

You can get every greek character (lower and upper case) thanks to this procedure:

由于此过程,您可以获得每个希腊字符(小写和大写):

Sub x()
    Dim i As Long

    For i = 913 To 969
        With Cells(i - 912, 1)
            .Formula = "=dec2hex(" & i & ")"
            .Offset(, 1).Value = ChrW$(i)
        End With
    Next i
End Sub

You can create an array to find the char for instance.

例如,您可以创建一个数组来查找字符。

Source: http://www.excelforum.com/excel-programming/636544-adding-greek-letters.html

资料来源:http: //www.excelforum.com/excel-programming/636544-adding-greek-letters.html

[EDIT 2] Here is a sub to build the string you wanted:

[编辑 2] 这是一个构建你想要的字符串的子:

Sub greekAlpha()
Dim sAlpha As String
Dim lLetter As Long

For lLetter = &H3B1 To &H3C9
    sAlpha = sAlpha & ChrW(lLetter)
Next
End Sub

回答by z??

As previously mentioned, VBA does support unicode strings, however you cannot write unicode strings inside your code, because the VBA editor only allows VBA files to be encoded in the 8-bit codepage Windows-1252.

如前所述,VBA 确实支持 unicode 字符串,但是您不能在代码中写入 unicode 字符串,因为 VBA 编辑器只允许在 8 位代码页中对 VBA 文件进行编码Windows-1252

You can however convert a binary equivalent of the unicode string you wish to have:

但是,您可以转换您希望拥有的 unicode 字符串的二进制等效项:

str = StrConv("±23′μ?·1o?????àá????é", vbFromUnicode)
'str value is now "αβγδεζηικλμνξοπρστθφω"

Use notepad to convert the string: copy-paste the unicode string, save the file as unicode (not utf-8) and open it as ASCII (which is in fact Windows-1252), then copy-paste it into the VBA editor without the first two characters (?t), which is the BOM marker

使用记事本转换字符串:复制粘贴 unicode 字符串,将文件另存为 unicode(不是 utf-8)并以 ASCII 格式打开(实际上是 Windows-1252),然后将其复制粘贴到 VBA 编辑器中,无需前两个字符 (?t),即 BOM 标记

回答by tricasse

You say that your source is interpreted as "áa?????éê?ìí??e?ó???ù".

你说你的来源被解释为“áa?????éê?ìí??e?ó???ù”。

Note that the Visual Basic Editor doesn't displayUnicode, but it does support manipulating Unicode strings:

请注意,Visual Basic 编辑器不显示Unicode,但它支持操作 Unicode 字符串

Dim strValue As String
strValue = Range("A1").Value
Range("B1").Value = Mid(strValue, 3)
Range("C1").Value = StrReverse(strValue)

If A1 contains Greek characters, B1 and C1 will contain Greek characters too after running this code.

如果 A1 包含希腊字符,则运行此代码后 B1 和 C1 也将包含希腊字符。

You just can't view the values properly in the Immediate window, or in a MsgBox.

您只是无法在立即窗口或 MsgBox 中正确查看值。