Vietnamese Unicode characters string error (showing "?") - excel

I am using Excel VBA, I have an error when I assign Vietnamese-Unicode character values in variable or MsgBox, like this:
Range("A1").Value = "Nguyễn Văn Bình"
MsgBox(Sheet5.Range("a453").Value
It results with: Nguy?n V?n B?nh
Is there a way to work around this?

Related

Why does using [#Name] in Excel VBA string cause error 1004?

When I try to execute this statement using Excel VBA, it gives an error 1004:
Cells(C, 2).Value = "=IFERROR(XLOOKUP([#Name],Master_List_1[Name],Master_List_1[Email]),""error"")"
If I remove the brackets around #Name, it works fine. The other brackets in the string don't cause trouble. Somehow VBA is parsing the text string and objecting to having [#Name] in there.
If I put the statement in a cell by hand (with single quotes), it works as it should. (I did forget the = sign; that's been corrected.

Excel Unicode appearing literally in cell value

I've got strings in an Excel table that have literal unicode values, i.e.   and I'm trying to compare them to another string which instead of the unicode string, has simply a space.
How do I do this? I tried this:
textref = replace(textref, ChrW$(160), " ")
as well as
textref = replace(textref, " ", " ")
and I can't seem to get it to work. Alternatively, is there a way to make Excel render the text as spaces?
Thanks in advance

Turkish Excel version: transforming dotted upper i, "İ" to dotless upper i "I" with VBA

I identified a problem of special characters with “I” on a turkish version of Windows 10 with a Turkish Excel version. “i” gives another letter in Turkish when it is translated to uppercase: “İ” and not “I”, as for instance when "i" is converted to upper case in English.
The problem is that when I use non case sensitive Excel search formulas (for instance “match” or “countif” formulas), the Turkish Excel will look for “i” or “İ” (with a dot), which it doesn’t find as the letters are uppercase in the lookup range (and there's no "İ"), while the English version look for “i” or “I”, which they’ll find.
To summarize, I search for "i" in a non-case sensitive way, and my Turkish colleague doesn't find any result, because his computer looks for "i" and "İ", and all the expected results are with "I".
I cannot ask the users having this problem to change the language in Excel or in Windows, nor change the lookup source or target ranges. But I can change the formulas (match and countifs are used).
I'm not sure where this lower to uppercase conversion is coming from, if it's from Excel or Windows. But after installing a Turkish version of Excel on a German version of Windows, I didn't have the problem. So I presume the problem is coming from Windows (some language settings, in the end, knowing that is interesting but won't help much)...
I was thinking to writ a VBA formula to change texts to uppercase AND change dotted capital “İ” to dotless capital “I” with replace, then use this string in my search functions. But I cannot find the dotted capital “İ” in the Chr() formula... I think the Chr() function doesn't use the extended ASCII characters, just the standard ones. See the kind of function I intended to use below.
Function upper_i(myStr As String) As String
upper_i = UCase(myStr)
upper_i = Replace(upper_i, Chr(???), "I")
End Function
How can I tell Excel I want a dotted capital "İ" here?
Thanks for your help!
Assuming I'm reading correctly, maybe try:
Function upper_i(myStr As String) As String
upper_i = UCase$(myStr)
upper_i = Replace(upper_i, ChrW(304), "I")
End Function
Seems to pass the test below at least:
Private Sub TestFunction()
Dim someText As String
someText = "ok, ok, " & ChrW(304)
Debug.Assert someText <> "OK, OK, I"
someText = upper_i(someText)
Debug.Assert someText = "OK, OK, I"
End Sub
I didn't really understand why you're making the string uppercase, but maybe I need to read your question a few more times.

Excel error: expected end of statement, what does this mean?

I am trying to write this formula into a cell via my script:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"t","f")"
And it keeps coming up as an error, even though it works just fine in the actual sheet if I manually input it into the cell. What is it expecting me to do here?
You need to escape quotes. Try this:
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),""t"",""f"")"
The issue that you are running into is that " is interpreted as beginning or ending a VBA string. So VBA parses your expression as
strFormulas(1) = "=IF(AND(I2<12.2,I2>=8.2),"
with "garbage" at the end. This "garbage" is what it is complaining about. If you need to include a " within a VBA string, use "".

VBA Special characters U+2264 and U+2265

I have a frustrating problem. I have a string containg other characters that are not in this list (check link). My string represents a SQL Query.
This is an example of what my string can contain: INSERT INTO test (description) VALUES ('≤ ≥ >= <=')
When I check the database, the row is inserted successfully, but the characters "≤" and "≥" are replaced with "=" character.
In the database the string in description column looks like "= = >= <=".
For the most characters I can get a character code. I googled a character code for those two symbols, but I didn't find one. My goal is to check if my string contains this two characters , and afterwards replace them with ">=" and "<="
===Later Edit===
I have tried to check every character in a for loop;
tmp = Mid$(str, i, 1)
tmp will have the value "=" when my for loop reaches the "≤" character, so Excel cannot read this "≤" character in a VB string, then when I'm checking for character code I get the code for "=" (Chr(61))
Are you able to figure out what the character codes for both "≤" and "≥" in your database character set are? if so then maybe try replacing both characters in your query string with chrw(character_code).
I have just tested something along the lines of what you are trying to do using Excel as my database - and it looks to work fine.
Edit: assuming you are still stuck and looking for assistance here - could you confirm what database you are working with, and any type information setting for the "description" field you are looking to insert your string into?
Edit2: I am not familiar with SQL server, but isn't your "description" field set up to be of a certain data type? if so what is it and does it support unicode characters? ncharvar, nchar seem to be examples of sql server data types that support Unicode.
It sounds like you may also want to try and add an "N" prefix to the value in your query string - see
Do I have use the prefix N in the "insert into" statement for unicode? &
how to insert unicode text to SQL Server from query window
Edit3: varchar won't qualify for proper rendering of Unicode - see here What is the difference between varchar and nvarchar?. Can you switch to nvarchar? as mentionned above, you may also want to prefix the values in your query string with 'N' for full effect
Edit4: I can't speak much more about sqlserver, but what you are looking at here is how VBA displays the character, not at how it actually stores it in memory - which is the bottom line. VBA won't display "≤" properly since it doesn't support the Unicode character set. However, it may - and it does - store the binary representation correctly.
For any evidence of this, just try and paste back the character to another cell in Excel from VBA, and you will retrieve the original character - or look at the binary representation in VBA:
Sub test()
Dim s As String
Dim B() As Byte
'8804 is "≤" character in Excel character set
s = ChrW(8804)
'Assign memory representation of s to byte array B
B = s
'This loop prints "100" and "34", respectively the low and high bytes of s coding in memory
'representing binary value 0010 0010 0110 0100 ie 8804
For i = LBound(B) To UBound(B)
Debug.Print B(i)
Next i
'This prints "=" because VBA can not render character code 8804 properly
Debug.Print s
End Sub
If I copy your text INSERT INTO test (description) VALUES ('≤ ≥ >= <=') and paste it into the VBA editor, it becomes INSERT INTO test (description) VALUES ('= = >= <=').
If I paste that text into a Excel cell or an Access table's text field, it pastes "correctly".
This seems to be a matter of character code supported, and I suggest you have a look at this SO question.
But where in you program does that string come from, since it cannot be typed in VBA ??
Edit: I jus gave it a try with the below code, and it works like a charm for transferring your exotic characters from the worksheet to a table !
Sub test1()
Dim db As Object, rs As Object, cn As Object
Set cn = CreateObject("DAO.DBEngine.120")
Set db = cn.OpenDatabase("P:\Database1.accdb")
Set rs = db.OpenRecordset("table1")
With rs
.addnew
.Fields(0) = Range("d5").Value
.Update
End With
End Sub

Resources