Special characters (letters čćžšđ) in Excel 2013 VBA code

我怕爱的太早我们不能终老 提交于 2020-01-05 02:31:27

问题


I made a program in Excel 2010 VBA that contains letters like ć č š...

Msgbox("Čiča gliša") works.

Looks like Excel 2013 supports those letters in cells and formulas, but not in VBA.

VBA replaces them with some symbols which aren't even on the keyboard.

I get errors executing the code.

I believe it's something to do with language settings.


回答1:


As BambiLongGone stated, weird string are not likely to work. I would say your best shot is looking at this article. There 's a tool called Unicode to VBA that you can use to convert all your string in your application. For example :

Čiča gliša

will be converted to

ChrW$(&H10C) & "i" & ChrW$(&H10D) & "a gli" & ChrW$(&H161) & "a"



回答2:


VBA is ANSI. Ps in it's interactions with Windows. It's UTF16 internaly and COM is also UTF 16. But it's file format is also ANSI so typing wierd strings are not likely to work (because they can't be saved as is).

So character conversion happen automatically with a million rules controlling it (mostly undocumented in an accessible fashion).

If in trouble assign to a byte array. Maybe you bneed toread from unicode file to bypass form's ANSI.

Yourstring() = "blah blah"

VB treats byte arrays as strings if passed to string functions.




回答3:


You can write manually the accents in every piece of code they appear, and you can use the "find & replace" to do it faster. If you have something like:

MsgBox "Código único"

Then you can find and replace:

[ó] to [" & Chr(243) & "]

[ú] to [" & Chr(250) & "]

And so on...

And you will get:

MsgBox "C" & Chr(243) & "digo " & Chr(250) & "nico"

If you don't know the code for each accented letter, then you can use excel with the function "CODE" (Function Char does the opposite)

Also, you could use a list from the internet like this one:

ASCII Code - The extended ASCII table

I just had the same problem and did this procedure. Worked fine using Visual Studio Code and very fast.




回答4:


Problem is in windows regional settings: Region/Administrative/Language for non-unicode programs, which must be set to language that can handle your special characters.




回答5:


Function CroatianCharacters(CroatianCharacterOrderNumber As Integer, Optional UpperCase As Boolean = True, Optional DisplayAll As Boolean = False) As String

   'ISO Latin1 Extended A decimal code
    Dim AllCharacters
    UpperCaseLetters = "A,B,C," & ChrW(268) & "," & ChrW(262) & "," & "D," & "D" & ChrW(382) & "," & ChrW(272) & "," & _
        "E,F,G,H,I,J,K,L,Lj,M,N,Nj,O,P,R,S," & ChrW(352) & ",T,U,V,Z," & ChrW(381)
    LowerCaseLetters = "a,b,c," & ChrW(269) & "," & ChrW(263) & "," & "d," & "d" & ChrW(382) & "," & ChrW(273) & "," & _
        "e,f,g,h,i,j,k,l,lj,m,n,nj,o,p,r,s," & ChrW(353) & ",t,u,v,z," & ChrW(382)
    CroatianCharacters = LowerCaseLetters

    If (UpperCase = True) Then
        If (DisplayAll = False) Then
            AllCharacters = Split(UpperCaseLetters, ",")
            CroatianCharacters = AllCharacters(CroatianCharacterOrderNumber - 1)
        Else
            CroatianCharacters = UpperCaseLetters
        End If
    Else
        If (DisplayAll = False) Then
            AllCharacters = Split(LowerCaseLetters, ",")
            CroatianCharacters = AllCharacters(CroatianCharacterOrderNumber - 1)
        Else
            CroatianCharacters = LowerCaseLetters
        End If
    End If

End Function


来源:https://stackoverflow.com/questions/27418058/special-characters-letters-%c4%8d%c4%87%c5%be%c5%a1%c4%91-in-excel-2013-vba-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!