Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert unicode character into a VBA string

Tags:

unicode

vba

Using MS Access 2003 on Windows 7, I found that the following function strips all accents from ANSI strings, changing (for example) señor to senor:

Public Function RemoveAccents(ByVal inputString As String) As String
Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóóôõöùúûüýÿ"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaaceeeeiiiionoooooouuuuyy"
Dim i As Integer

For i = 1 To Len(accentString)
inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
Next i
RemoveAccents = inputString
End Function

But when I tried to add Latin small letter C with Caron (U-010D)(č)(HTML č) to the function Const accentString, like this,

Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW$(&H10D) & "èéêëìíîïðñòóóôõöùúûüýÿ"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"

I was unable to run the function. Is there a syntax which will allow me to adapt this function to strip the diacriticals from strings containing characters not in the ANSI character set?

like image 776
Commata Avatar asked Mar 26 '13 16:03

Commata


People also ask

How do you insert a character in a string in Excel VBA?

Use the Left and Right string functions and concatenate the three parts together with & . Left(Range("A1"). Text, 7) - this returns the first seven characters. Right(Range("A1").

How do you add a Unicode to a string?

In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.

How do I insert a Unicode character?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

What is ChrW in VBA?

The ChrW function returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function.


1 Answers

You can't have functions like ChrW() in a constant declaration.

Revised

If you can make these Public variables instead of Constants, then this should take care of it for you:

Const cWithCaron As String = &H10D
Public accentString As String
Public nonAccentStr As String


Sub TestStrings()
Dim clnString As String

    accentString = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW(cWithCaron) & "èéêëìíîïðñòóóôõöùúûüýÿ"

    nonAccentStr = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"
    'I added this variable to test the function:
    clnString = RemoveAccents(accentString)
    'And a message box to display the results:
    MsgBox clnString = nonAccentStr

End Sub

Public Function RemoveAccents(ByVal inputString As String) As String

Dim i As Integer

    For i = 1 To Len(accentString)

        inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
    Next i

    RemoveAccents = inputString

End Function
like image 72
David Zemens Avatar answered Oct 22 '22 23:10

David Zemens