VBA 函数无法在 excel 2007 中返回阿拉伯字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9177412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA function cannot return arabic string in excel 2007?
提问by medo ampir
i had a VBA function that returns arabic string (it is for converting a number into arabic writen string)
我有一个返回阿拉伯字符串的 VBA 函数(用于将数字转换为阿拉伯文字字符串)
i was using it on excel 2007 on windows xp
我在 windows xp 上的 excel 2007 上使用它
but when i changed to windows 7 the function started to return some thing like this:
但是当我更改为 Windows 7 时,该函数开始返回如下内容:
YT? ??óé ?á?Y ? ???????é ? ??èúé ? ???èú?? ? 59 á??í?
是吗???óé ?á?Y ? ???????é ??èúé ? ???欧洲联盟??? 第59话
i can write arabic in vba editor but can't view it in excel cell.
我可以在 vba 编辑器中编写阿拉伯语,但无法在 excel 单元格中查看它。
回答by jdh
Excel stores strings in Unicode. The string your are using appears to be using the ANSI Arabic (Windows) codeset. In your previous windows XP setup, you probably had this language settings enabled and haven't set it up in Windows 7. If I save your string to a text file, and import it in Excel using the Data (import text) wizard, specifiying the input code page as ANSI Arabic, it returns: ??? ???? ???? ? ???????? ? ????? ? ??????? ? 59 ????? (Which Google translates equates to: "Only five thousand and eight hundred and forty-four and 59 to change my"). You probably would want to convert your old text from a specific code page (suspected ANSI Arabic) to the more flexible UTF-8 to completely avoid require setting up individual code pages.
Excel 以 Unicode 格式存储字符串。您使用的字符串似乎使用 ANSI 阿拉伯语 (Windows) 代码集。在您之前的 Windows XP 设置中,您可能启用了此语言设置,但尚未在 Windows 7 中进行设置。如果我将您的字符串保存到文本文件中,然后使用数据(导入文本)向导将其导入 Excel,输入代码页为 ANSI 阿拉伯语,它返回: ??? ??????? ????????? ? ????? ? ??????? ? 59 ???(谷歌翻译成:“只有五千八百四十四和 59 才能改变我的”)。您可能希望将旧文本从特定代码页(疑似 ANSI 阿拉伯语)转换为更灵活的 UTF-8,以完全避免需要设置单独的代码页。
Its unclear where you are getting your Arabic strings from. Maybe a database or maybe hardcoded in your old VBA code. Ideally its from a database, and you can do a one time conversion there (to convert those strings to Unicode). Then your VBA code can consistently use Unicode as the native encoding.
不清楚您从何处获取阿拉伯语字符串。可能是数据库,也可能是旧 VBA 代码中的硬编码。理想情况下,它来自数据库,您可以在那里进行一次转换(将这些字符串转换为 Unicode)。然后您的 VBA 代码可以始终使用 Unicode 作为本机编码。
Alternatively, if you have to dynamically convert the ANSI Arabic strings in your code, you can use the vba function StrConv() using a paramter to specify what encoding your string is in (from this table). This routine is a little unfriendly, in that you have to convert the input string to a byte array first. Here's an example of how to do this conversion:
或者,如果您必须在代码中动态转换 ANSI 阿拉伯语字符串,您可以使用 vba 函数 StrConv() 使用参数来指定您的字符串采用的编码(来自此表)。此例程有点不友好,因为您必须先将输入字符串转换为字节数组。以下是如何进行此转换的示例:
Function convertANSIArabic2Unicode(inputStr As String) As String
Dim n As Integer
Dim i As Integer
Dim inBytes() As Byte
Dim sUnicode As String
' Convert input string to byte array
n = Len(inputStr)
ReDim inBytes(n + 1)
For i = 1 To n
inBytes(i) = AscB(Mid(inputStr, i, 1))
Next
' Convert byte array to unicode using Arabic coding
sUnicode = StrConv(inBytes, vbUnicode, &H401)
' remove starting null
iPos = InStr(sUnicode, Chr(0))
If iPos > 0 Then sUnicode = Mid(sUnicode, iPos + 1)
convertANSIArabic2Unicode = sUnicode
End Function
Where it would be called like this:
它会像这样被调用:
Dim xStr As String
xStr = "YT? ??óé ?á?Y ? ???????é ? ??èúé ? ???èú?? ? 59 á??í?"
ActiveCell = convertANSIArabic2Unicode(xStr)
Would result in the cell displaying:
会导致单元格显示:
??? ???? ???? ? ???????? ? ????? ? ??????? ? 59 ?????
回答by user3367687
Under Window 7 OS
在 Window 7 操作系统下
STEP 1: GOTO CONTROL PANEL AND SELECT REGION AND LANGUAGE
STEP 2: SELECT ADMINISTRATIVE TAB
CHOOSE BUTTON SYSTEM LOCAL LANGUAGE
COMPUTER WILL SHOW YOU A SCREEN TO SELECT ANY ARABIC LANG
STEP 3: SELECT YOUR LOCAL LANGUAGE CLOSE ALL
NOW YOU GOTO VBA EDITOR PAGE YOU WILL BE HAPPY
回答by M.M.Rame
in VB Editor: - 1- click tools 2- Select Options 3- click Editor Format 4- Change font to(Courier New (Arabic)) 5- click OK now you can write in both English and Arabic I hope this will solve your problem
在 VB 编辑器中: - 1- 单击工具 2- 选择选项 3- 单击编辑器格式 4- 将字体更改为(Courier New (Arabic)) 5- 单击确定现在您可以用英语和阿拉伯语书写我希望这能解决您的问题