17

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 "áâãäåæçéêëìíîïðñóôõöù"
is there any way use unicode at declaration level?

5
  • "convert greek characters to latin" What does this mean? α becomes a, β becomes b, γ becomes g etc.? If so, what should ζ, η, ξ be converted to? Sep 2, 2011 at 9:43
  • I have my own Target string, where I have the conversions. It basically, doesn't matter..
    – Stavros
    Sep 9, 2011 at 7:34
  • Related: this method cannot be used in const. See excel - Unicode string literals in VBA - Stack Overflow ___________________________________________________________________________________________ Some functions (MsgBox for example) doesn't support Unicode -- see ms access - How do I display a messagebox with unicode characters in VBA? - Stack Overflow
    – user202729
    Sep 28, 2021 at 14:44
  • @GSerg Perhaps you should reverse the duplicate closure? This one is both higher voted and older.
    – user202729
    Sep 28, 2021 at 14:51
  • 1
    @user202729 Neither is relevant. What is relevant is the correctness of the answers. The accepted answer here is not correct as it produces double Unicode which doesn't make sense and corrupts the data, and to add insult to injury, it does so based on the regular string literal which, due to the non-Unicodeness of the IDE, will only even appear to work only on a computer with Greek locale, as I noted under that answer.
    – GSerg
    Sep 28, 2021 at 15:02

3 Answers 3

14

You can try StrConv:

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

Source : 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

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

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

For lLetter = &H3B1 To &H3C9
    sAlpha = sAlpha & ChrW(lLetter)
Next
End Sub
8
  • 1
    Still, it doesn't work. maybe it's the way I am declaring the variable. Have you managed to make this work?
    – Stavros
    Sep 1, 2011 at 13:43
  • @Stavros: indeed, i couldn't make it work in a full example. i added another solution (which works - depending on what you want to do)
    – JMax
    Sep 1, 2011 at 14:09
  • I don't want to use Cells from the spreadsheet. Everything should be in VB code. and it's not the whole alphabet that I want to convert. Only the letters I have in my example as Source.
    – Stavros
    Sep 2, 2011 at 8:45
  • @Stavros: i built a procedure that will create the string with the right character but as i still don't know what you are trying to achieve, i can only assess an try...
    – JMax
    Sep 2, 2011 at 8:59
  • 4
    StrConv("string literal", vbUnicode) is absolutely wrong. What it does: it first creates a Unicode string containing the literal (and if the literal contained characters not representable in the current ANSI codepage, it will already be garbage at this point), then converts it to Unicode again, pretending that it was in ANSI. This results in a "double Unicode" string. For English-only strings, it looks like there is a vbNullChar inserted after each character; for national strings, the result is complete garbage.
    – GSerg
    Jul 8, 2019 at 15:08
12

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.

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

str = StrConv("±²³´µ¶·¹º»¼½¾¿ÀÁÃĸÆÉ", 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 (ÿþ), which is the BOM marker

2
  • 4
    A clever trick, but it suffers from exactly the same problem. The resulting clever characters may easily be not representable in the current ANSI codepage of the computer. E.g. when I paste that string into my VBA editor, I get "±???µ¶·??»????AAAA??E".
    – GSerg
    Jul 8, 2019 at 15:14
  • I was able to make this method work to some extent in Notepad++.. open new file in Notepad++, change encoding to UCS-2, paste unicode text, change encoding to Windows-1252, it will ask to save file, save it. Close in Notepad++ and open file in Windows Notepad, while opening select Encoding as ANSI. Use this text in VBA as mentioned above. However, problem comes when the ANSI text has the double quote that is used as string delimited in VBA. For few character, VBA ChrW() function can also be used with unicode hex converted to decimal.. e.g. to embed Unicode minus sign "−" use ChrW(8722).
    – Uttam
    Aug 29, 2022 at 18:40
8

You say that your source is interpreted as "áâãäåæçéêëìíîïðñóôõöù".

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

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.

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

1
  • 1
    Note that while MsgBox won't display Unicode correctly, you can display your Unicode string in a Label or TextBox control on a UserForm. Sep 12, 2023 at 19:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.