MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Non Latin Characters

Non-Latin Text in VBA Code


In spreadsheet cell A1, we have the following Arabic pangram:

صِف خَلقَ خَودِ كَمِثلِ الشَمسِ إِذ بَزَغَت — يَحظى الضَجيعُ بِها نَجلاءَ مِعطارِ

VBA provides the AscW and ChrW functions to work with multi-byte character codes. We can also use Byte arrays to manipulate the string variable directly:

Sub NonLatinStrings()
Dim rng As Range
Set rng = Range("A1")
Do Until rng = ""
 Dim MyString As String
 MyString = rng.Value

 ' AscW functions
 Dim char As String
 char = AscW(Left(MyString, 1))
 Debug.Print "First char (ChrW): " & char
 Debug.Print "First char (binary): " & BinaryFormat(char, 12)

 ' ChrW functions
 Dim uString As String
 uString = ChrW(char)
 Debug.Print "String value (text): " & uString ' Fails! Appears as '?'
 Debug.Print "String value (AscW): " & AscW(uString)

 ' Using a Byte string
 Dim StringAsByt() As Byte
 StringAsByt = MyString
 Dim i As Long
 For i = 0 To 1 Step 2
 Debug.Print "Byte values (in decimal): " & _
 StringAsByt(i) & "|" & StringAsByt(i + 1)
 Debug.Print "Byte values (binary): " & _
 BinaryFormat(StringAsByt(i)) & "|" & BinaryFormat(StringAsByt(i + 1))
 Next i
 Debug.Print ""
 ' Printing the entire string to the immediate window fails (all '?'s)
 Debug.Print "Whole String" & vbNewLine & rng.Value
 Set rng = rng.Offset(1)
Loop
End Sub

This produces the following output for the Arabic Letter Sad:

First char (ChrW): 1589
First char (binary): 00011000110101
String value (text): ?
String value (AscW): 1589
Byte values (in decimal): 53|6
Byte values (binary): 00110101|00000110

Whole String
??? ????? ????? ??????? ??????? ??? ??????? — ????? ???????? ???? ??????? ???????

Note that VBA is unable to print non-Latin text to the immediate window even though the string functions work correctly. This is a limitation of the IDE and not the language.

Conclusion

In this page (written and validated by ) you learned about VBA Non Latin Characters . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA API Calls.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.