我正在逐行将文本文件与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 :(得分: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