2014-05-07 61 views
4

我在VBA中读取一个相当大的文本文件。源文件包含来自国际会议的各种文本数据,并且我将这些数据的提取部分以有组织的格式重写为Excel工作表。我已经将例程的重要部分放在下面。 (我是一个原始程序员,所以我使用原始方法,对不起我的不良形式)如何将VBA中的非ASCII字符写入Excel工作表?

问题来自不是简单的ASCII字符的字符的位置。文本文件包含各种条目,如“慕尼黑”。当写入Excel文件时,文本会严重受损。在这种情况下,我得到了“München”。真烦人的是我可以从文本文件中复制文本,并将其粘贴到Excel中,没有问题。但是有太多的记录需要手动完成。我非常希望将原始(非ASCII)字符写入电子表格!

我花了相当长的时间寻找解决方案,并且找到了两个相关的线程。首先,在这里的第一个答案中描述了一种名为“binaryStream”的东西: Excel VBA - export to UTF-8 它不够接近我想要做的事情,我无法弄清楚我需要的部分。

还有Save text file UTF-8 encoded with VBA,它描述了如何将UTF-8数据写入文本文件。差不多,但我想写一个Excel电子表格!

有什么建议吗?

Sub ParseText() 
    Dim myFile As String, Author1 As String, Author2 As String 
    Dim dept As String, inst As String, country As String 
    Dim title As String, LineText As String 
    Dim nSourceFile As Integer, NewRecord As Boolean 
    Dim First13 As String, nn As Integer, Row As Integer, LineText2 As String 

    myFile = Application.GetOpenFilename() 
    nSourceFile = FreeFile 
    Open myFile For Input As #nSourceFile 
    Row = 2 
    While Not EOF(nSourceFile) 
     Line Input #nSourceFile, LineText ''This is a blank line 
     Line Input #nSourceFile, LineText ''This has both first and last name 
     Author1 = Right(LineText, Len(LineText) - 2) 
     Author2 = RTrim(Left(Author1, Len(Author1) - 1)) 
     LineText = Author2 
     nn = InStrRev(LineText, " ") 
     Author1 = RTrim(Left(LineText, nn)) 
     Author2 = RTrim(Right(LineText, Len(LineText) - nn)) 
     nn = Len(Author2) 
     LineText = LCase(Right(Author2, nn - 1)) 
     Author2 = Left(Author2, 1) & LineText 
     Cells(Row, 1) = Author1 
     Cells(Row, 2) = Author2 

     Row = Row + 1 
    Wend '' reached the end of file 


End Sub 
+1

你见过这种可能吗? http://www.ozgrid.com/forum/showthread.php?t=164547 – dee

+1

你也可以将源文件转换为UTF-16LE(也称为Windows内部的Unicode,记事本可以将其转换),然后在读取VBA代码时转换字符串与'StrConv'和'vbFromUnicode'标志 –

+0

我不知道它的理想..但我用vba替换所有非ascii字符与原始字符,因为即使我没有找到任何解决方案。例如:用ü替换Ã – neophyte

回答

0

答案在给予和OP接受:

“你也可以转换的源文件为UTF-16LE的VBA中读取数据时(在Windows里又名统一,记事本可以把它),然后代码将字符串转换为StrConv和vbFromUnicode标志“ - z̫͋