2010-08-27 59 views
0

alt text獲取問號字符讀取文件中的行時行到工作表

我讀通過VBA行的文本文件一行到工作表(?)。它通常會返回這些字符。有誰知道如何去除它們?

這是怎麼了我讀它:

Sub ReadAsciiFile() 

    Dim sFileName As String 
    Dim iFileNum As Integer 
    Dim sBuf As String 
    Dim i As Integer 
    i = 1 

    ' edit this: 
    sFileName = "usmap.txt" 

    ' does the file exist? simpleminded test: 
    If Len(Dir$(sFileName)) = 0 Then 
     Exit Sub 
    End If 

    iFileNum = FreeFile() 
    Open sFileName For Input As iFileNum 

    Do While Not EOF(iFileNum) 
     Line Input #iFileNum, sBuf 
     ' now you have the next line of the file in sBuf 
     ' do something useful: 
     activesheet.Cells(i, 1) = sBuf 
     i = i + 1 
     Debug.Print sBuf 
    Loop 

    ' close the file 
    Close iFileNum 

End Sub 
+0

他們看起來像他們的不可打印表示(在當前字符集)字符。原文是什麼? – Robaticus 2010-08-27 19:07:20

+0

原始文本是 – 2010-08-27 19:07:52

回答

1

首先,如果你複製並粘貼到Excel中,檢查你的粘貼方法,並嘗試「選擇性粘貼」爲文本(而不是HTML或Unicode文本)。

您還可以使用簡單的VBA函數來找出無效字符是什麼。然後,您可以使用另一個VBA函數刪除無效字符,或使用我在上一個問題中顯示的Excel公式函數Substitute()。下面是一些VBA代碼一個簡單的例子,看看有什麼角色存在於細胞A1

Sub PrintChars() 
    Dim intLen As Integer 
    Dim str As String 
    str = Range("A1").Value 
    intLen = Len(str) 
    Dim intCount As Integer 
    intCount = 1 

    Dim strChar As String 

    Do While intCount <= intLen 
    strChar = Mid(str, intCount, 1) 
    Debug.Print strChar & ": " & Asc(strChar) 
    intCount = intCount + 1 
    Loop 

End Sub 
+0

+1漢堡包圖片 – 2010-08-27 19:17:52

+0

@I__ ????漢堡包? – 2010-08-28 00:36:37