如何处理西里尔字母在工作表(Unicode VBA显示和处理)中导致错误(#REF),在VBA

这个问题相当复杂。 您可以在下面find代码的精髓。 我想从一个特定的工作簿中获取数据(有几十个,循环部分工作正常),但下面的代码不起作用,因为工作表是在西里尔文,他们给的价值'? ?????” sheet As String参数。 我想问如何强制重命名工作簿而不打开它(没有Workbook.Open(WorkbookNum1)因为在这种情况下需要相当多的时间)。 或者如何修改这个代码来应用表索引,例如: sheets(3) 。 我将衷心感谢您的帮助!

 Private Function GetValue(path, file, sheet, ref) Dim arg As String 'Make sure the file exists If Right(path, 1) <> "\" Then path = path & "\" If Dir(path & file) = "" Then GetValue = "File Not Found" Exit Function End If 'Create the argument arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ Range(ref).Range("A1").Address(, , xlR1C1) 'Execute an XLM macro GetValue = ExecuteExcel4Macro(arg) End Function Sub TestGetValue() p = "c:\XLFiles\Budget" f = "Budget.xls" s = "Sheet1" a = "A1" MsgBox GetValue(p, f, s, a) End Sub 

VBA很好地处理unicode,但VBA编辑器不显示它。 这可以使unicode很难在VBA中工作,如果你不习惯。

当第一次面对与unicode的工作需要(大多数人使用随机小波为可爱的文字效果,但也是一个整体的俄罗斯网站),我解决了这个问题,把unicodestring放在工作表(工作表显示unicode就好了),然后设置通过从工作表中提取他们的值来调整我的variables。

因此,build立一个叫做“UniSrc”的工作表(名字可以是任何东西),在列A到D中包含path,文件,表单和地址。在列C中复制和粘贴unicode表名。

这里是你的testing程序,修改为以这种方式工作:

 Sub TestGetValue() dim rowNow as long, wks as excel.worksheet dim p as string, f as string, s as string, a as string rowNow = 2 ' assuming row 1 is a header set wks = thisworkbook.sheets("UniSrc") with wks p = .cells(rowNow, 1) ' path f = .cells(rowNow, 2) ' file name s = .cells(rowNow, 3) ' unicode name of sheet sought a = .cells(rowNow, 4) ' cell address sought end with MsgBox GetValue(p, f, s, a) End Sub 

现在我们需要更新你的GetValue函数:

 Function GetValue(sPath as string, sFile as string, sSheet as string, sAddress as string) as variant Dim arg As String, vTmp as variant If Right(sPath, 1) <> "\" Then sPath = sPath & "\" If Dir(sPath & sFile) = "" Then ' Make sure the file exists vTmp = "File Not Found" else ' Create the argument arg = "'" & sPath & "[" & sFile & "]" & sSheet & "'!" & _ Range(sAddress).Range("A1").Address(, , xlR1C1) 'Execute an XLM macro vTmp = ExecuteExcel4Macro(arg) End If GetValue = vTmp End Function 

看你怎么走。 我在我的平板电脑上,所以我把它写在了我的头顶,无法testing,但必需品在那里。

VBA中的Unicode字符可以被硬编码为一系列ChrW函数连接。

 MsgBox ChrW(1044) & ChrW(1040) ' "ДА" 

为了得到unicode的代码点 ,你可以例如把工作表名称放在A1并尝试如下所示:

 Dim b() As Byte, i As Long, a As Long b = [A1].Text ' converts the string to byte array (2 or 4 bytes per character) For i = 1 To UBound(b) Step 2 ' 2 bytes per Unicode codepoint a = b(i) a = a * 256 + b(i - 1) Debug.Print " & AscW(" & a & ")"; Next