在excel 2007中VBA函数无法返回阿拉伯字符串?(VBA function cannot return arabic string in excel 2007?)

我有一个返回阿拉伯字符串的VBA函数(它用于将数字转换为阿拉伯字符串)

我在Windows XP上使用它在Excel 2007上

但是当我改变到Windows 7的功能开始返回一些这样的事情:

ÞÞØÎÉÉÇÇÇÇÇÇÉÇÉÉÇAÑÈËÉÇÈÑÇÈÑÇÉÇÇÇÇÑÑÑÑ

我可以在vba编辑器中编写阿拉伯文,但无法在Excel单元格中查看它。

i had a VBA function that returns arabic string (it is for converting a number into arabic writen string)

i was using it on excel 2007 on windows xp

but when i changed to windows 7 the function started to return some thing like this:

ÝÞØ ÎãÓÉ ÂáÇÝ æ ËãÇäãÇÆÉ æ ÃÑÈÚÉ æ æÃÑÈÚæä æ 59 áÇÛíÑ

i can write arabic in vba editor but can't view it in excel cell.

最满意答案

Excel以Unicode存储字符串。 您正在使用的字符串似乎使用ANSI阿拉伯语(Windows)代码集。 在以前的Windows XP安装程序中,可能启用了此语言设置,并且尚未在Windows 7中对其进行设置。如果将字符串保存为文本文件,并使用数据(导入文本)向导将其导入Excel,输入代码页作为ANSI阿拉伯语,它返回:فقطخمسةآلافوثمانمائةوأربعةوورربعون和59لاغير(其中谷歌翻译等于:“只有五千八百四十四和59改变我的”)。 您可能希望将旧文本从特定代码页(疑似ANSI阿拉伯语)转换为更灵活的UTF-8,以完全避免需要设置单个代码页。

它不清楚你从哪里得到你的阿拉伯字符串。 也许是您的旧VBA代码中的数据库或硬编码。 理想情况下,它来自数据库,并且可以在那里进行一次转换(将这些字符串转换为Unicode)。 然后你的VBA代码可以一直使用Unicode作为本地编码。

或者,如果必须在代码中动态转换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

它会被称为这样的地方:

Dim xStr As String xStr = "ÝÞØ ÎãÓÉ ÂáÇÝ æ ËãÇäãÇÆÉ æ ÃÑÈÚÉ æ æÃÑÈÚæä æ 59 áÇÛíÑ" ActiveCell = convertANSIArabic2Unicode(xStr)

将导致单元显示:

فقط خمسة آلاف و ثمانمائة و أربعة و وأربعون و 59 لاغير

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.

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.

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:

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 = "ÝÞØ ÎãÓÉ ÂáÇÝ æ ËãÇäãÇÆÉ æ ÃÑÈÚÉ æ æÃÑÈÚæä æ 59 áÇÛíÑ" ActiveCell = convertANSIArabic2Unicode(xStr)

Would result in the cell displaying:

فقط خمسة آلاف و ثمانمائة و أربعة و وأربعون و 59 لاغير

更多推荐